Anda di halaman 1dari 10

KSOU BScIT 5th sem Lab program

KSOU BScIT 5th sem jave Lab programms /* * Program using switch statement to compute the net amount to be paid by customer */ import java.io.*; class BSIT_53L_1 { public final static double DISCOUNT_ON_T_SHIRT = 0.1; public final static double DISCOUNT_ON_SILK_SAREE = 0.3; public final static double DISCOUNT_ON_BABA_SUIT = 0.4; public final static double DISCOUNT_ON_TROUSERS = 0.15; public final static double COST_OF_T_SHIRT = 299.99; public final static double COST_OF_SILK_SAREE = 499.99; public final static double COST_OF_BABA_SUIT = 999.95; public final static double COST_OF_TROUSERS = 649.95; public static void main( String args[] ) { System.out.println( "ITEM \t\t\t DISCOUNT \t\t PRICE" ); System.out.println( "---- \t\t\t -------- \t\t -----" ); System.out.println( "1. T SHIRT \t\t 10% \t\t\t" + COST_OF_T_SHIRT ); System.out.println( "2. SILK SAREE \t\t 30% \t\t\t" + COST_OF_SILK_SAREE ); System.out.println( "3. BABA SUIT \t\t 40% \t\t\t" + COST_OF_BABA_SUIT ); System.out.println( "4. TROUSERS \t\t 15% \t\t\t" + COST_OF_TROUSERS ); System.out.print( "Please Enter your Item : " ); BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); BufferedReader br1 = new BufferedReader( new InputStreamReader( System.in ) ); int count = 0; int noOfItems = 0; double amountByCustomer = 0.00d; try { count = Integer.parseInt( br.readLine() ); switch ( count ) { case 1: System.out.print( "Please enter number of T Shirts : " ); noOfItems = Integer.parseInt( br1.readLine() ); amountByCustomer = noOfItems * ( COST_OF_T_SHIRT - ( COST_OF_T_SHIRT * DISCOUNT_ON_T_SHIRT ) ); break; case 2: System.out.print( "Please enter number of SILK SAREE : " ); noOfItems = Integer.parseInt( br1.readLine() ); amountByCustomer = noOfItems * ( COST_OF_SILK_SAREE - ( COST_OF_SILK_SAREE * DISCOUNT_ON_SILK_SAREE ) ); break; case 3: System.out.print( "Please enter number of BABA SUIT : " ); noOfItems = Integer.parseInt( br1.readLine() ); amountByCustomer = noOfItems * ( COST_OF_BABA_SUIT - ( COST_OF_BABA_SUIT * DISCOUNT_ON_BABA_SUIT ) ); break; case 4: System.out.print( "Please enter number of TROUSERS : " ); noOfItems = Integer.parseInt( br1.readLine() ); amountByCustomer = noOfItems * ( COST_OF_TROUSERS - ( COST_OF_TROUSERS * DISCOUNT_ON_TROUSERS ) );

KSOU BScIT 5th sem Lab program 2 break; } System.out.println( "Amount Payable by customer is : " + Math.rint( amountByCustomer * 100 ) / 100 ); } catch ( Exception e ) { System.out.println( "error" ); System.exit( 1 ); } } } /* * Program to represent a bank account. */ import java.io.*; class BSIT_53L_2_3 { private String nameOfDepositer; private long accountNumber; private String typeOfAccount; private long balanceAmount; public static BSIT_53L_2_3 assignInitialValue( BSIT_53L_2_3 accountDetail ) { return accountDetail; } //constructor public BSIT_53L_2_3( ) { this.nameOfDepositer = ""; this.accountNumber = 0; this.typeOfAccount = ""; this.balanceAmount = 0; } public static BSIT_53L_2_3 depositAmount( BSIT_53L_2_3 accountDetail ) { try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Deposit Amount: " ); accountDetail.balanceAmount += Long.parseLong(br.readLine()); } catch (IOException ioe) { System.out.println("IO error"); System.exit(1); } return accountDetail; } public static BSIT_53L_2_3 withdrawAmount( BSIT_53L_2_3 accountDetail ) { try{ System.out.println( "Balance Available : " + accountDetail.balanceAmount ); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Amount To Withdraw : " ); accountDetail.balanceAmount -= Long.parseLong(br.readLine()); } catch (IOException ioe) { System.out.println("IO error"); System.exit(1); } return accountDetail; }

KSOU BScIT 5th sem Lab program 3 public static void displayNameAndBalance( BSIT_53L_2_3 accountDetail ) { System.out.println("Name : " + accountDetail.nameOfDepositer ); System.out.println("Balance Available : " + accountDetail.balanceAmount ); } public static void main(String args[]) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { BSIT_53L_2_3 accountDetail = new BSIT_53L_2_3(); System.out.println("Please enter Account Depositer Details "); System.out.print("Enter Name Of Account Holder: " ); accountDetail.nameOfDepositer = br.readLine(); System.out.print("Enter Account Number: " ); accountDetail.accountNumber = Long.parseLong(br.readLine()); System.out.print("Enter Type Of Account: " ); accountDetail.typeOfAccount = br.readLine(); System.out.print("Enter Amount: " ); accountDetail.balanceAmount = Long.parseLong(br.readLine()); while( true ) { System.out.println( "Option 1 : Deposit an Amount"); System.out.println( "Option 2 : Withdraw After Checking Balance"); System.out.println( "Option 3 : Display Name and Balance"); System.out.println( "Option 4 : To Exit"); System.out.println( "Please select options from menu: "); int choice = Integer.parseInt( br.readLine() ); switch( choice ) { case 1: accountDetail = depositAmount( accountDetail ); break; case 2: accountDetail = withdrawAmount( accountDetail ); break; case 3: displayNameAndBalance( accountDetail ); break; case 4: System.exit(1); } } } catch (IOException ioe) { System.out.println("IO error"); System.exit(1); } } } /* * Program to illustrate importing classes from other packages */ import java.util.*; class BSIT_53L_5{ public static void main(String[] args) { Vector v = new Vector(); for (int i = 0; i < 10; ++i) v.addElement(new Integer(i)); for (int i = 0; i < v.size(); ++i) System.out.print(v.elementAt(i) + " "); } }

KSOU BScIT 5th sem Lab program /* * Program to illustrate multiple inheritance */ interface Monster { void menace(); } interface DangerousMonster extends Monster { void destroy(); } interface Lethal { void kill(); } class DragonZilla implements DangerousMonster { public void menace() { } public void destroy() { } } interface Vampire extends DangerousMonster, Lethal { void drinkBlood(); } class VeryBadVampire implements Vampire { public void menace() { } public void destroy() { } public void kill() { } public void drinkBlood() { } } public class BSIT_53L_6 { static void u(Monster b) { b.menace(); } static void v(DangerousMonster d) { d.menace(); d.destroy(); } static void w(Lethal l) { l.kill(); } public static void main(String[] args) { DangerousMonster barney = new DragonZilla(); u(barney); v(barney); Vampire vlad = new VeryBadVampire(); u(vlad); v(vlad); w(vlad); } } /* * Program to illustrate subclasses an imported class

KSOU BScIT 5th sem Lab program 5 */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class BSIT_53L_7 { public static void main(String[] args) { try { Employee[] staff = new Employee[3]; Employee harry = new Employee("Harry Sample", 35000); staff[0] = harry; staff[1] = new Manager("Carl Java", 75000, harry); staff[2] = new Manager("Tony Java2s", 38000, harry); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("employee.dat")); out.writeObject(staff); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream( "employee.dat")); Employee[] newStaff = (Employee[]) in.readObject(); for (int i = 0; i < newStaff.length; i++) newStaff[i].raiseSalary(100); for (int i = 0; i < newStaff.length; i++) newStaff[i].print(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } class Employee implements Serializable { public Employee(String n, double s) { name = n; salary = s; } public Employee() { } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public void print() { System.out.println(name + " " + salary); } private String name; private double salary; } class Manager extends Employee { private Employee secretary; public Manager(String n, double s, Employee e) { super(n, s); secretary = e; }

KSOU BScIT 5th sem Lab program public Manager() { } public void raiseSalary(double byPercent) { super.raiseSalary(byPercent + 10); } public void print() { super.print(); System.out.print("Secretary: "); if (secretary != null) secretary.print(); } } /* * Program for creating threads using thread class */ public class BSIT_53L_8 extends Thread { private Thread creatorThread; public BSIT_53L_8() { creatorThread = Thread.currentThread(); } public void run() { for ( int i = 0; i < 10; i++ ) { printMsg(); } } public void printMsg() { Thread t = Thread.currentThread(); if ( t == creatorThread ) { System.out.println("Creator thread"); } else if ( t == this ) { System.out.println("New thread"); } else { System.out.println("Unexpected threads!"); } } public static void main(String[] args) { BSIT_53L_8 tt = new BSIT_53L_8(); tt.start(); for ( int i = 0; i < 10; i++ ) { tt.printMsg(); } }

} /* * Program to illustrate the use of yield(), sleep(), and stop() methods */ public class BSIT_53L_9 extends Object implements Runnable { public void run() { try { System.out.println("in run() - sleep for 20 seconds"); Thread.sleep(20000); System.out.println("in run() - woke up"); } catch (InterruptedException x) {

KSOU BScIT 5th sem Lab program 7 System.out.println("in run() - interrupted while sleeping"); return; } System.out.println("in run() - leaving normally"); } public static void main(String[] args) { BSIT_53L_9 si = new BSIT_53L_9(); Thread t = new Thread(si); t.start(); // Be sure that the new thread gets a chance to // run for a while. try { Thread.yield(); Thread.sleep(2000); } catch (InterruptedException x) { } System.out.println("in main() - interrupting other thread"); t.stop(); System.out.println("in main() - leaving"); } } /* * Program to illustrate the effects of assigning higher priority to a thread */ public class BSIT_53L_10 extends Object { private volatile int count; private boolean yield; private Thread internalThread; private volatile boolean noStopRequested; public BSIT_53L_10(String name, int priority, boolean yield) { count = 0; this.yield = yield; noStopRequested = true; Runnable r = new Runnable() { public void run() { try { runWork(); } catch (Exception x) { x.printStackTrace(); } } }; internalThread = new Thread(r, name); internalThread.setPriority(priority); } private void runWork() { Thread.yield(); while (noStopRequested) { if (yield) { Thread.yield(); } count++; for (int i = 0; i < 1000; i++) { double x = i * Math.PI / Math.E; } } } public void startRequest() { internalThread.start(); }

KSOU BScIT 5th sem Lab program 8 public void stopRequest() { noStopRequested = false; } public int getCount() { return count; } public String getNameAndPriority() { return internalThread.getName() + ": priority=" + internalThread.getPriority(); } private static void runSet(boolean yield) { BSIT_53L_10[] pc = new BSIT_53L_10[3]; pc[0] = new BSIT_53L_10("P0", 3, yield); pc[1] = new BSIT_53L_10("P1", 6, yield); pc[2] = new BSIT_53L_10("P2", 6, yield); try { Thread.sleep(1000); } catch (InterruptedException x) { } for (int i = 0; i < pc.length; i++) { pc[i].startRequest(); } long startTime = System.currentTimeMillis(); try { Thread.sleep(10000); } catch (InterruptedException x) { } for (int i = 0; i < pc.length; i++) { pc[i].stopRequest(); } long stopTime = System.currentTimeMillis(); try { Thread.sleep(1000); } catch (InterruptedException x) { } int totalCount = 0; for (int i = 0; i < pc.length; i++) { totalCount += pc[i].getCount(); } System.out.println("totalCount=" + totalCount + ", count/ms=" + roundTo(((double) totalCount) / (stopTime - startTime), 3)); for (int i = 0; i < pc.length; i++) { double perc = roundTo(100.0 * pc[i].getCount() / totalCount, 2); System.out.println(pc[i].getNameAndPriority() + ", " + perc + "%, count=" + pc[i].getCount()); } } public static double roundTo(double val, int places) { double factor = Math.pow(10, places); return ((int) ((val * factor) + 0.5)) / factor; } public static void main(String[] args) { Runnable r = new Runnable() { public void run() {

KSOU BScIT 5th sem Lab program 9 System.out.println("Run without using yield()"); System.out.println("========================="); runSet(false); System.out.println(); System.out.println("Run using yield()"); System.out.println("================="); runSet(true); } }; Thread t = new Thread(r, "Priority"); t.setPriority(Thread.MAX_PRIORITY - 1); t.start(); }} /* * Program for creating threads using Runnable interface */ public class BSIT_53L_11 implements Runnable { private int countDown = 5; public String toString() { return "#" + Thread.currentThread().getName() + ": " + countDown; } public void run() { while(true) { System.out.println(this); if(--countDown == 0) return; } } public static void main(String[] args) { for(int i = 1; i <= 5; i++) new Thread(new BSIT_53L_11(), "" + i).start(); // Output is like SimpleThread.java }} /* * Program to illustrate the use of try and catch for exception handling */ public class BSIT_53L_12 { public static void main(String[] args) { try { try { int c[] = {0, 1, 2, 3}; c[4] = 4; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index out of bounds: " + e); } } catch(ArithmeticException e) { System.out.println("Divide by zero: " + e); } }} /* * Program to illustrate the use of wrapper class methods */ import java.util.*; public class BSIT_53L_13{

KSOU BScIT 5th sem Lab program 10 public static void main(String argv[]){ Vector v = new Vector(); v.add(new Integer(1)); v.add(new Integer(2)); for(int i=0; i < v.size();i ++){ Integer iw =(Integer) v.get(i); System.out.println(iw.intValue()); } }} /* * Program to illustrate the use of wrapper class methods */ import java.util.*; public class BSIT_53L_14{ public static void main(String argv[]){ try { int c[] = { 1 }; c[42] = 99; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("array index oob: " + e); } double c; try { c = 20/0; System.out.println("a divided by b : " + c); } catch (ArithmeticException exp ) { System.out.println("Arithmetic error"); System.exit(1); } }} /* * Program to illustrate the implemetation of interfaxces as class types */ interface A { } interface B { } class X implements A, B { } class Y implements A { B makeB() { // Anonymous inner class: return new B() { }; }} public class BSIT_53L_15 { static void takesA(A a) { } static void takesB(B b) { } public static void main(String[] args) { X x = new X(); Y y = new Y(); takesA(x); takesA(y); takesB(x); takesB(y.makeB()); }}

Anda mungkin juga menyukai