You are on page 1of 12

JAVA Assignmemt-2

Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent, switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes import java.io.*; class washingmachine { public static void main(String[] args) throws IOException { washingmachine w=new washingmachine(); w.switchOn(); w.acceptClothes(); } void switchOn() { System.out.println("Machine Switched on"); } void switchOff() { System.out.println("Machine Switched off"); } void acceptClothes() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter no of clothes"); int n=Integer.parseInt(br.readLine()); System.out.println("No of clothes: "+n); } void acceptDetergent() throws IOException { ufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter detergent"); String str=br.readLine(); System.out.println("Detergent used: "+str); } }

Create a calculator class which will have methods add, multiply, divide & subtract import java.io.*; class calculator { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { calculator c=new calculator(); BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.println("To perform Addition press 1"); System.out.println("To perform substraction press 2"); System.out.println("To perform Multiplication press 3"); System.out.println("To perform Division press 4"); int ch=Integer.parseInt(b.readLine()); switch(ch) { case 1: c.add(); break; case 2: c.sub(); break; case 3: c.mul(); break; case 4: c.div(); break; default: System.exit(0); } } } void add() throws IOException { System.out.println("Enter first no"); int a=Integer.parseInt(br.readLine()); System.out.println("Enter second no"); int b=Integer.parseInt(br.readLine()); System.out.println("Add: "+(a+b)); } void sub() throws IOException { System.out.println("Enter first no"); int a=Integer.parseInt(br.readLine()); System.out.println("Enter second no"); int b=Integer.parseInt(br.readLine()); System.out.println("Sub: "+(a-b));

} void mul() throws IOException { System.out.println("Enter first no"); int a=Integer.parseInt(br.readLine()); System.out.println("Enter second no"); int b=Integer.parseInt(br.readLine()); System.out.println("Mul: "+(a*b)); } void div() throws IOException { System.out.println("Enter first no"); int a=Integer.parseInt(br.readLine()); System.out.println("Enter second no"); int b=Integer.parseInt(br.readLine()); System.out.println("Div: "+(a/b)); } } I. Create a class called Student which has the following methods: Average: which would accept marks of 3 examinations & return whether the student has passed or failed depending on whether he has scored an average above 50 or not. II. Inputname: which would accept the name of the student & returns the name. import java.io.*; class Student { String name; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void studentName() throws IOException { System.out.println("Enter student name"); name=br.readLine(); System.out.println("Student Name: "+name); } void average() throws IOException { System.out.println("Enter marks for first exam"); int m1=Integer.parseInt(br.readLine()); System.out.println("Enter marks for second exam"); int m2=Integer.parseInt(br.readLine()); System.out.println("Enter marks for third exam"); int m3=Integer.parseInt(br.readLine()); int m=(m1+m2+m3)/3; if(m>50) System.out.println(name+" pass"); else System.out.println(name+" fail"); }

} class Studenttest { public static void main(String[] args) throws IOException { Student std=new Student(); std.studentName(); std.average(); } } . Create a Bank class with methods deposit & withdraw. The deposit method would accept attributes amount & balance & returns the new balance which is the sum of amount & balance. Similarly, the withdraw method would accept the attributes amount & balance & returns the new balance balance amount if balance > = amount or return 0 otherwise. import java.io.*; class bank { double balance,amount; bank() { balance=500; } public double deposit(double amt) { balance=balance+amt; return balance; } public double withdraw(double amt) { if(balance>=amt) { balance=balance-amt; return balance; } else return 0; } } class banktest { public static void main(String[] args) throws IOException { bank b=new bank(); double bal; double amt; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

while(true) { System.out.println("To Deposit press 1"); System.out.println("To Withdraw press 2"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: System.out.println("Enter amount to deposit"); amt=Double.parseDouble(br.readLine()); bal=b.deposit(amt); System.out.println("Balance: "+bal); break; case 2: System.out.println("Enter amount to withdraw"); amt=Double.parseDouble(br.readLine()); bal=b.withdraw(amt); System.out.println("Balance: "+bal); break; default: System.exit(0); } } } }

Create an Employee class which has methods netSalary which would accept salary & tax as arguments & returns the netSalary which is tax deducted from the salary. Also it has a method grade which would accept the grade of the employee & return grade. import java.io.*; class employee { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); double netpay(double sal,double tax) { return (sal-tax); } String grade() throws IOException { System.out.println("Enter grade for employee"); String grade=br.readLine(); return grade; } } class employeetest { public static void main(String[] args) throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); employee e=new employee(); System.out.println("Enter salary"); double sal=Double.parseDouble(b.readLine()); System.out.println("Enter tax"); double tax=Double.parseDouble(b.readLine()); System.out.println("Net Pay: "+e.netpay(sal,tax)); System.out.println("Grade: "+e.grade()); } }

Create Product having following attributes: Product ID, Name, Category ID and UnitPrice. Create ElectricalProduct having the following additional attributes: VoltageRange and Wattage. Add a behavior to change the Wattage and price of the electrical product. Display the updated ElectricalProduct details. import java.io.*; class product { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int pid; String pname,category; int unitprice; void setproductattribute() throws IOException { System.out.println("Enter product id"); pid=Integer.parseInt(br.readLine()); System.out.println("Enter product name"); pname=br.readLine(); System.out.println("Enter product category"); category=br.readLine(); System.out.println("Enter product unit price"); unitprice=Integer.parseInt(br.readLine()); } void showproduct() { System.out.println("Product System.out.println("Product System.out.println("Product System.out.println("Product } } class electricalproduct extends product { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int voltagerange=240; int wattage=500; void set() throws IOException { super.setproductattribute() ; System.out.println("Enter new voltagerange"); voltagerange=Integer.parseInt(br.readLine()); } void changewattage() throws IOException { System.out.println("Enter new wattage value"); int wattage=Integer.parseInt(br.readLine()); System.out.println("new wattage: "+wattage);

Id: "+pid); Name: "+pname); Category: "+category); Unit Price: "+unitprice);

} void show() { super.showproduct(); System.out.println("Product Wattage: "+wattage); System.out.println("Product Voltage Range: "+voltagerange); } } class producttest { public static void main(String[] args) throws IOException { electricalproduct ep=new electricalproduct(); ep.set(); ep.show(); } } Create Book having following attributes: Book ID, Title, Author and Price. Create Periodical which has the following additional attributes: Period (weekly, monthly etc...) .Add a behavior to modify the Price and the Period of the periodical. Display the updated periodical details. import java.io.*; class Book { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int bid; String title,author; double price; void set() throws IOException { System.out.println("Enter Book id"); bid=Integer.parseInt(br.readLine()); System.out.println("Enter Title"); title=br.readLine(); System.out.println("Enter author"); author=br.readLine(); System.out.println("Enter price"); price=Double.parseDouble(br.readLine()); } void show() { System.out.println("Book id: "+bid); System.out.println("Title: "+title); System.out.println("Author: "+author); System.out.println("price: "+price); } }

class periodical extends Book { String period; void set() throws IOException { super.set(); System.out.println("Enter period"); period=br.readLine(); } void changePrice() throws IOException { System.out.println("Enter changed price"); price=Double.parseDouble(br.readLine()); } void changePeriod() throws IOException { System.out.println("Enter changed period"); period=br.readLine(); } void show() { super.show(); System.out.println("period: "+period); } } class Booktest { public static void main(String[] args) throws IOException { Book b=new Book(); b.set(); b.show(); periodical p=new periodical(); p.set(); p.changePrice(); p.show(); } } Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional attributes:loading capacity( 100 tons).Add a behavior to change the color and loading capacity. Display the updated truck details. import java.io.*; class vehicle { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int vno; String model,manufacturer,color;

void set() throws IOException { System.out.println("Enter vehicle no: "); vno=Integer.parseInt(br.readLine()); System.out.println("Enter model: "); model=br.readLine(); System.out.println("Enter manufacturer: "); manufacturer=br.readLine(); System.out.println("Enter Color: "); color=br.readLine(); } void show() { System.out.println("Vehicle No: "+vno); System.out.println("Model: "+model); System.out.println("manufacturer: "+manufacturer); System.out.println("Color: "+color); } } class Truck extends vehicle { int loadingcap; void set() throws IOException { super.set(); System.out.println("Enter loading capacity:"); loadingcap=Integer.parseInt(br.readLine()); } void show() { super.show(); System.out.println("Loading Capacity: "+loadingcap); } } class vehicletest { public static void main(String[] args) throws IOException { Truck tk=new Truck(); tk.set(); tk.show(); } } Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results.

import java.io.*; class raiseToPower { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); raiseToPower rtp=new raiseToPower(); System.out.println("Enter number"); int n1=Integer.parseInt(br.readLine()); System.out.println("Enter power"); int p1=Integer.parseInt(br.readLine()); int res=rtp.raise(n1,p1); System.out.println("Result :"+res); System.out.println("Enter decimal number"); double n2=Double.parseDouble(br.readLine()); System.out.println("Enter power"); int p2=Integer.parseInt(br.readLine()); double res2=rtp.raise(n2,p2); System.out.println("Result :"+res2); } public int raise(int n,int p) { int res=1; for(int i=1;i<=p;i++) { res=res*n; } return res; } public double raise(double n,int p) { double res=1; for(int i=1;i<=p;i++) { res=res*n; } return res; } } Write a function Model-of-Category for a Tata motor dealers, which accepts category of car customer is looking for and returns the car Model available in that category. the function should accept the following categories "SUV", "SEDAN", "ECONOMY", and "MINI" which in turn returns "TATA SAFARI" , "TATA INDIGO" , "TATA INDICA" and "TATA NANO" respectively.

import java.io.*; class modelOfCategory { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); modelOfCategory moc=new modelOfCategory(); System.out.println("CATEGORIES Available: "); System.out.println("SUV"); System.out.println("SEDAN"); System.out.println("ECONOMY"); System.out.println("MINI"); System.out.println("Enter category"); String cat=br.readLine(); System.out.println("Model for "+cat+" is:"+moc.model(cat)); } String model(String cat) { if(cat.equalsIgnoreCase("SUV")) return "TATA SAFARI"; if(cat.equalsIgnoreCase("SEDAN")) return "TATA INDIGO"; if(cat.equalsIgnoreCase("ECONOMY")) return "TATA INDICA"; if(cat.equalsIgnoreCase("MINI")) return "TATA NANO"; else return "Invalid category"; } }

You might also like