User:Sachi4g/sandbox

//Singleton Pattern public class Login { private static Login instance = null; public Login {		System.out.println("The Login Instance Created"); }	public static Login getInstance {		if(instance==null) {			instance= new Login; }		return instance; }	public boolean validateUser(String username, String password) {		return username.equals(password); }

}

public class LoginTest {	public static void main(String args[]) {		System.out.println(Login.getInstance.validateUser("Manju", "Manju")); }

}

-- //Thread

public class PrintThread extends Thread{

public void run {		for(int r=1; r<=100; r++) {			System.out.println(r); }	} }

public class SLIITThread implements Runnable{

public void run {		for(int i=1; i<=100; i++) {			System.out.println("SLIIT"); }	} }

public class ThreadMain {	public static void main(String args[]) {		PrintThread thread1 = new PrintThread; SLIITThread thread2 = new SLIITThread; thread1.run; thread2.run; } } -

public class NumbersThread {

public void run {		for(int r=1; r<=100; r++) {			System.out.println("Thread Name: " +Thread.currentThread.getName +" " +r); }	} }

public class ThreadBase { public static void main(String args[]) {		ThreadBase thread1 = new ThreadBase; ThreadBase thread2 = new ThreadBase; ThreadBase thread3 = new ThreadBase; thread1.setName("Red"); thread2.setName("Blue"); thread3.setName("Green"); thread1.run; thread2.run; thread3.run; }

private void setName(String string) { // TODO Auto-generated method stub }	public void run {		for(int r=1;r<=100;r++) {			System.out.println(r); //System.out.println("Thread Status:" +thread1.getState+ "-isAlive="+thread1.isAlive); }	}

} --- public class Calculation { float total; public Calculation {		total=0; }	public void sum(int start, int end) {		float tot=0; for (int r=start; r<=end; r++) {			tot=tot+r; }		synchronized(this) {			total=total+tot; }	}	public float getTotal {		return total; }

}

public class BaseThread extends Thread{ Calculation myCalc; int start,end; public BaseThread(Calculation calc, int end, int start) {		myCalc= calc; this.end=end; this.start=start; }	public void run {		myCalc.sum(start, end); }

}

public class ParallalThread {	public ParallalThread(Calculation mycalc, int start, int end) { // TODO Auto-generated constructor stub }

public static void main(String args[]) throws InterruptedException {			Calculation mycalc =new Calculation; ParallalThread thread1 = new ParallalThread(mycalc, 1, 50000); ParallalThread thread2 = new ParallalThread(mycalc,50001, 100000); thread1.start; thread2.start; thread1.join; thread2.join; System.out.println("The Total:" +mycalc.getTotal); }

private void join {		// TODO Auto-generated method stub }

private void start {		// TODO Auto-generated method stub }

} - //Abstract

public abstract class Vehicle {	private double speed; public double regularPrice; private String color; public Vehicle(double speed, String color) {		super; this.speed=speed; this.color=color; }	public Vehicle (double speed, String color, double regularPrice) {		super; this.speed=speed; this.color=color; this.regularPrice=regularPrice; }	public abstract double getRegularPrice;

} public class Truck extends Vehicle {	private double weight;

public Truck(double speed, String color,double weight) {		super(speed, color); this.weight= weight; // TODO Auto-generated constructor stub }

@Override public double getRegularPrice {		// TODO Auto-generated method stub double salesPrice= 0; if(weight>2000) {			salesPrice = (super.regularPrice*90)/100; }		else {			salesPrice = (super.regularPrice*80)/100; }		return salesPrice; } }

public class Bus extends Vehicle {	private int year; private double manufactureDiscount;

public Bus(double speed, String color, int year, double manufactureDiscount) {		super(speed, color); this.year=year; this.manufactureDiscount=manufactureDiscount; // TODO Auto-generated constructor stub }

@Override public double getRegularPrice { // TODO Auto-generated method stub double salesPrice; salesPrice= (super.regularPrice*(100-manufactureDiscount))/100; return salesPrice; } }

public class MyOwnAutoShop {	public static void main(String args[]) {		Vehicle vehicle1 = new Truck(100, "Red", 1000); System.out.println(Truck.getRegularPrice); Vehicle vehicle2 = new Bus(180, "Blue", 2015, 0.25); System.out.println(Bus.getRegularPrice); }

} --- //AbstractFactory Design Pattern

public abstract class MobilePhone {	public String model; public double price; public abstract void Display; }

public class A10 extends MobilePhone {	public A10 {		super.model="A10"; super.price=25000; }

@Override public void Display { // TODO Auto-generated method stub System.out.println("The Mobile Model is:" +super.model+ "and Price is:"+super.price);

} }

public class TPlus extends MobilePhone {	public TPlus {		super.model="TPlus"; super.price=45000; }	@Override public void Display { // TODO Auto-generated method stub System.out.println("The Mobile Model is:" +super.model+ "and Price is:"+super.price); }

}

public class X25 extends MobilePhone {

public X25 {		super.model="X25"; super.price=35000; }	@Override public void Display { // TODO Auto-generated method stub System.out.println("The Mobile Model is:" +super.model+ "and Price is:"+super.price);

} }

public abstract class TV { public String model; public double size; public abstract void Display;

}

public class Alpha40 extends TV {

public Alpha40 {		super.model="Alpha40"; super.size=16.2; }	@Override public void Display { // TODO Auto-generated method stub }

}

public class Gamma50 extends TV {

public Gamma50 {		super.model="Gamma50"; super.size=12; }	@Override public void Display { // TODO Auto-generated method stub }

}

public class Theta65 extends TV {

public Theta65 {		super.model="Theta65"; super.size=36; }	@Override public void Display { // TODO Auto-generated method stub } }

public abstract class AbstractFactory {	//public abstract TV getTVmodel(String model); //public abstract MobilePhone getMobilePhonemodel(String model);

}

public class FactoryDemo {

public static void main(String[] args) { FactoryProducer.getFactory("TV").getTVmodel("Alpha40").display; FactoryProducer.getFactory("TV").getTVmodel("Gamma50").display; FactoryProducer.getFactory("TV").getTVmodel("Theta65").display; FactoryProducer.getFactory("Phone").getPhoneModel("A10").display; FactoryProducer.getFactory("Phone").getPhoneModel("X25").display; FactoryProducer.getFactory("Phone").getPhoneModel("TPlus").display; }

} -- //Inheritance class Shape { protected String name; public Shape {}; public Shape (String tname) { name = tname; }   public void print { System.out.println("\nThen name of shape is: " + name); }		    public int area{ return 0;} } class Rectangle extends Shape { protected int width; protected int height; public Rectangle (String tname, int w, int h) { super(tname); width = w;       height = h;    }

public int area { return width * height; }

public void print { super.print; System.out.println("\nThe height of rectangle is: " + height); System.out.println("\nThe width of rectangle is: " + width); } } class Circle extends Shape { protected int radius; public Circle (String tname, int r) { super(tname); radius = r;   } public int area { return (int) (22/7.0 * radius* radius); }   public void print { super.print; System.out.println("\nThe radius of circle is: " + radius); } } class ShapeApp { public static void main(String args[]) { Rectangle R = new Rectangle("Rectangle", 4, 6); Circle C = new Circle("Circle", 3 );

R.print; System.out.println("\nThe area of rectangle is: " + R.area);

System.out.println("\n\n*****************************");

C.print; System.out.println("\nThe area of circle is: " + C.area); } }

//Interfaces public interface Sports { public void setHomeTeam(String name); public void setVisitingTeam(String name); }

// Filename: Football.java public interface Football extends Sports { public void homeTeamScored(int points); public void visitingTeamScored(int points); public void endOfQuarter(int quarter); }

// Filename: Hockey.java public interface Hockey extends Sports { public void homeGoalScored; public void visitingGoalScored; public void endOfPeriod(int period); public void overtimePeriod(int ot); } -- //Constructors class Students { String name; String gender; int age;

Students(String n, String g, int a) { name= n; gender= g; age= a; } } class testconstructor { public static void main(String[]args) { students student1= new students("Bob", "Male", 13);

System.out.println("Student's name: " + student1.name); System.out.println("Student's gender: " + student1.gender); System.out.println("Student's age: " + student1.age); } } -- //Design Patterns --ThraedSafe Singleton public class TestThreadSingleton implements Runnable {	public static void main(String args[]) {		new Thread(new TestThreadSingleton).start; for(int i=0;i<10;i++) {			Singleton.getInstance; ThreadSafeSingleton.getInstance; }	}

public void run {		for(int i=0; i<=10; i++) {			Singleton.getInstance; ThreadSafeSingleton.getInstance; }	}

}

-- -- //Exception Handling import java.util.Scanner;

class InsufficientBalanceException extends Exception {

private double amount;

public InsufficientBalanceException(double amount) {		this.amount = amount; }

public double getAmount { return amount; } }

class Account {  private double balance; private int accNo; public Account(int accNo) {     this.accNo = accNo; } public double getBalance {     return balance; } public int getNumber {     return accNo; } public void deposit(double amount) {     balance += amount; }

public void withdraw(double amount) throws InsufficientBalanceException {     if(amount <= balance) {        balance -= amount; } else { double needs = amount - balance; throw new InsufficientBalanceException(needs); }  } }

class Demo { public static void main(String[] args) {

Account account = new Account(123); System.out.println("Depositing Rs.10,000"); account.deposit(10000.00);

try { continueTransaction(account);

} catch (InsufficientBalanceException e) { System.out.println("Sorry, your account remains only Rs." + e.getAmount); e.printStackTrace; } finally { System.out.println("Do you wish to continue? yes/no"); Scanner scanner = new Scanner(System.in); if(scanner.next.equalsIgnoreCase("yes")){ try { System.out.println("Depositing Rs.10,000"); account.deposit(10000.00); continueTransaction(account); } catch (InsufficientBalanceException e) { e.printStackTrace; }                }		 else{ System.out.println("Good Bye"); }	 }	}

public static void continueTransaction(Account account) throws InsufficientBalanceException {

System.out.print("Please enter amount to be withdrawn = "); Scanner scanner = new Scanner(System.in); double amount; while (true) { amount = scanner.nextDouble; System.out.print("\nWithdrawing Rs." + amount + "/="); account.withdraw(amount); System.out.println(" existing amount = " + account.getBalance); System.out.print("Please enter amount to be withdrawn = "); } }

}