Sie sind auf Seite 1von 20

Introduct|on to IAVA 8as|cs Ass|gnment 2

1 Create a wash|ng mach|ne c|ass w|th methods as sw|tchCn


acceptC|othes acceptDetergent sw|tchCff acceptC|othes
accepts the noofC|othes as argument returns the noofC|othes

Ans
import java.util.*;
class WashingMachine

Scanner inputnew Scanner(System.in);
public void switchOn ()

System.out.println ("The lid is open.");
}
public void start ()

System.out.println ("Start washing ...");
}
public void acceptDetergent ()

System.out.println("Adding Detergent.. ");
start();
}
public int acceptClothes()

System.out.println("Enter no oI clothes: ");
int noinput.nextInt();
return no;
}
public void switchOII ()

System.out.println ("The lid is closed.");
}
public static void main(String|| args)

WashingMachine wmnew WashingMachine();
wm.switchOn();
int numOFClotheswm.acceptClothes();
wm.acceptDetergent();
wm.switchOII();
System.out.println(numOFClothes" clothes get washed");
}
}





2 Create a ca|cu|ator c|ass wh|ch w||| have methods add mu|t|p|y
d|v|de subtract
Ans
import java.util.*;
class Calculation

public int add(int a,int b)

return ab;
}
public int subtract(int a,int b)

iI(a~b)

return a-b;
}
else

return b-a;
}
}
public int multiply(int a,int b)

return a*b;
}
public int divide(int a,int b)

iI(a~b)

return a/b;
}
else

return b/a;
}
}
}
public class Calculator

public static void main(String ||args)

Calculation calnew Calculation();
int addcal.add(5,10);
int subcal.subtract(5,10);
int mulcal.multiply(5,10);
int divcal.divide(5,10);
System.out.println(add);
System.out.println(sub);
System.out.println(mul);
System.out.println(div);
}
}

Create a c|ass ca||ed Student wh|ch has the fo||ow|ng methods
| Average wh|ch wou|d accept marks of exam|nat|ons return
whether the student has passed or fa||ed depend|ng on whether he
has scored an average above S0 or not
|| Inputname wh|ch wou|d accept the name of the student returns
the name
Ans
import java.util.*;
public class Student

Scanner inputnew Scanner(System.in);
public String average()

System.out.print("Enter Marks1: ");
double m1input.nextDouble();
System.out.print("Enter Marks2: ");
double m2input.nextDouble();
System.out.print("Enter Marks3: ");
double m3input.nextDouble();
double tmm1m2m3;
double avgtm/3;
iI(avg50)

return "Failed";
}
iI(avg~50)

return "Passed";
}
return " ";
}
public String getName()

System.out.println("Enter Name:");
String nameinput.nextLine();
String resultaverage();
return name" get "result;
}
public static void main(String||args)

Student datanew Student();
String nameAndResutdata.getName();
System.out.println(nameAndResut);
}
}
Create a 8ank c|ass w|th methods depos|t w|thdraw 1he depos|t
method wou|d accept attr|butes amount ba|ance returns the new
ba|ance wh|ch |s the sum of amount ba|ance S|m||ar|y the
w|thdraw method wou|d accept the attr|butes amount ba|ance
returns the new ba|ance 'ba|ance amount' |f ba|ance amount or
return 0 otherw|se
Ans
import javax.swing.*;
class Customer

int bal;
Customer(int bal)

this.bal bal;
}
int deposit(int amt)

iI (amt 0)

System.out.println("Invalid Amount");
return 1;
}
bal bal amt;
return 0;
}
int withdraw(int amt)

iI (bal amt)

System.out.println("Not suIIicient balance.");
return 1;
}
iI (amt 0)

System.out.println("Invalid Amount");
return 1;
}
bal bal - amt;
return 0;
}
void check()

JOptionPane.showMessageDialog(null,"Balance:"
Integer.toString(bal));
}
public class Bank

public static void main(String||args)

Customer Custnew Customer(1500);
String st1JOptionPane.showInputDialog(null,"Enter the amount
todeposit:");
int depInteger.parseInt(st1)
int bal1Cust.deposit(dep);
Cust.check();
String st2JOptionPane.showInputDialog(null,"Enter the amount to
withdraw:");
int withInteger.parseInt(st2);
int bal2Cust.withdraw(with);
Cust.check();
}}
S Create an Lmp|oyee c|ass wh|ch has methods netSa|ary wh|ch
wou|d accept sa|ary tax as arguments returns the netSa|ary wh|ch
|s tax deducted from the sa|ary A|so |t has a method grade wh|ch
wou|d accept the grade of the emp|oyee return grade
Ans
/*employee sal & tax*/
import java.util.*;
class Employee

static Scanner inputnew Scanner(System.in);
public double netSalary(double salary, double taxrate)

double taxsalary*taxrate;
double netpaysalarytax;
return netpay;
}
public static String grade( )

System.out.print("Enter Grade: ");
String gradeinput.next();
return grade;
}
public static void main(String|| args)

Employee empnew Employee();
System.out.print("Enter Salary: ");
double salinput.nextDouble();
System.out.print("Enter Tax in : ");
double taxrateinput.nextDouble()/100;
String gemp.grade();
double netemp.netSalary(sal,taxrate);
System.out.println("Net Salary is: "net);
System.out.println("Grade is: "g);
}
}

Create roduct hav|ng fo||ow|ng attr|butes roduct ID Name
Category ID and Un|tr|ce Create L|ectr|ca|roduct hav|ng the
fo||ow|ng add|t|ona| attr|butes Vo|tagekange and Wattage Add a
behav|or to change the Wattage and pr|ce of the e|ectr|ca| product
D|sp|ay the updated L|ectr|ca|roduct deta||s

Ans
import java.util.*;
class Product

int productID;
String name;
int categoryID;
double price;
Product(int productID,String name,int categoryID,double price)

this.productIDproductID;
this.namename;
this.categoryIDcategoryID;
this.priceprice;
}
}
public class ElectricalProduct extends Product

int voltageRange;
int wattage;
ElectricalProduct(int productID,String name,int categoryID,double price,int
voltageRange,
intwattage)

super(productID,name,categoryID,price);
this.voltageRangevoltageRange;
this.wattagewattage;
}
public void display()

System.out.println("Product ID: "productID);
System.out.println("Name: "name);
System.out.println("Category ID: "categoryID);
System.out.println("Price: "price);
System.out.println("Voltage Range: "voltageRange);
System.out.println("Wattage: "wattage);
}
public static void main(String|| args)

Scanner inputnew Scanner(System.in);
System.out.println("Enter Product ID: ");
int pidinput.nextInt();
System.out.println("Enter Name: ");
String nameinput.next();
System.out.println("Enter Catagory ID: ");
int cidinput.nextInt();
System.out.println("Enter Price: ");
double priceinput.nextDouble();
System.out.println("Enter Voltage Range: ");
int vrangeinput.nextInt();
System.out.println("Enter Wattage: ");
int wattageinput.nextInt();
System.out.println("****Details oI Electrical Product****");
System.out.println();
ElectricalProduct pnew ElectricalProduct(pid,name,cid,price,vrange,wattage);
p.display();
}
}

Create 8ook hav|ng fo||ow|ng attr|butes 8ook ID 1|t|e Author and
r|ce Create er|od|ca| wh|ch has the fo||ow|ng add|t|ona| attr|butes
er|od (week|y month|y etc) Add a behav|or to mod|fy the r|ce
and the er|od of the per|od|ca| D|sp|ay the updated per|od|ca|
deta||s

Ans
/*Book details*/
import java.util.*;
class Book

int id;
String title;
String author;
double price;
public void setId(int id)

this.idid;
}
public int getId()

return id;
}
public void setTitle(String title)

this.titletitle;
}
public String getTitle()

return title;
}
public void setAuthor(String author)

this.authorauthor;
}
public String getAuthor()

return author;
}
public void setPrice(double price)

this.priceprice;
}
public double getPrice()

return price;
}

}
class Periodical

String timeperiod;
public void setTimeperiod(String timeperiod)

this.timeperiodtimeperiod;
}
public String getTimeperiod()

return timeperiod;
}
}
public class BookInIormation

public static void main(String||args)

Book bnew Book();
Periodical pnew Periodical();
Scanner inputnew Scanner(System.in);
System.out.print("Book ID: ");
int idinput.nextInt();
b.setId(id);
System.out.print("Title: ");
String titleinput.next();
b.setTitle(title);
System.out.print("Author: ");
String authorinput.next();
b.setAuthor(author);
System.out.print("Price: ");
double priceinput.nextDouble();
b.setPrice(price);
System.out.print("Period: ");
String ppinput.next();
p.setTimeperiod(pp);
System.out.println();
System.out.println("----Book InIormation----");
System.out.println();
System.out.println("Book ID: "b.getId());
System.out.println("Title: "b.getTitle());
System.out.println("Author: "b.getAuthor());
System.out.println("Price: "b.getPrice());
System.out.println("Period: "p.getTimeperiod());
}
}


8 Create Veh|c|e hav|ng fo||ow|ng attr|butes Veh|c|e No Mode|
Manufacturer and Co|or Create truck wh|ch has the fo||ow|ng
add|t|ona| attr|butes|oad|ng capac|ty( 100 tons)Add a behav|or to
change the co|or and |oad|ng capac|ty D|sp|ay the updated truck
deta||s
Ans
class Vehicle

int no;
String model;
String manuIacturer;
String color;
Vehicle(int no,String model,String manuIacturer,String color)

this.nono;
this.modelmodel;
this.manuIacturermanuIacturer;
this.colorcolor;
}
}
public class Truck extends Vehicle

int capacity;
Truck(int no,String model,String manuIacturer,String color,int capacity)

super( no, model, manuIacturer, color);
this.capacitycapacity;
}
void show()

System.out.println("No " no);
System.out.println("Model " model);
System.out.println("manuIacturer " manuIacturer);
System.out.println("Color " color);
System.out.println("Capacity " capacity);
}
public static void main(String|| args)

Scanner inputnew Scanner(System.in);
System.out.println("Truck No: ");
int noinput.nextInt();
System.out.println("Model: ");
String modelinput.next();
System.out.println("ManuIacturer: ");
String manuIacturerinput.next();
System.out.println("Color: ");
String colorinput.next();
System.out.println("Loading Capacity: ");
int capinput.nextInt();
Truck tnew Truck(no,model,manuIacturer,color,cap);
System.out.println("****Truck Details****");
System.out.println();t.show();
}
}

Wr|te a program wh|ch performs to ra|se a number to a power and
returns the va|ue rov|de a behav|or to the program so as to accept
any type of numer|c va|ues and returns the resu|ts
Ans
import java.util.*;
import java.text.*;
class NumberProgram

public static void main(String|| args)

DecimalFormat dInew DecimalFormat("##.##");
Scanner inputnew Scanner(System.in);
System.out.print("Enter Number: ");
double numinput.nextDouble();
System.out.print("Raise Number's power: ");
double powinput.nextDouble();
double valueMath.pow(num,pow);
System.out.println("Result is: "dI.Iormat(value));
}}
10 Wr|te a funct|on Mode|ofCategory for a 1ata motor dea|ers
wh|ch accepts category of car customer |s |ook|ng for and returns the
car Mode| ava||ab|e |n that category the funct|on shou|d accept the
fo||ow|ng categor|es SUV SLDAN LCCNCM and MINI wh|ch
|n turn returns 1A1A SAIAkI 1A1A INDIGC 1A1A INDICA and
1A1A NANC respect|ve|y
Ans
/*car problem*/
import java.util.*;
class TataMotors

String category;
String model;
TataMotors(String category,String model)

this.categorycategory;
this.modelmodel;
}
public String getCategory()

return category;
}
public String getModel()

return model;
}
public static void ModelOICategory(ArrayListTataMotors~ list)

Scanner inputnew Scanner(System.in);
System.out.print("Enter Category: ");
String categoryinput.nextLine();
System.out.println();
System.out.print("Model is: ");
Ior (TataMotors tm : list)

iI(tm.getCategory().equals(category))

System.out.print(tm.getModel());
}
}
}
public static void main(String|| args)

ArrayListTataMotors~ listnew ArrayListTataMotors~();
list.add(new TataMotors("SUV","TATA SAFARI"));
list.add(new TataMotors("SEDAN","TATA INDIGO"));
list.add(new TataMotors("ECONOMY","TATA INDICA"));
list.add(new TataMotors("MINI","TATA NANO"));
ModelOICategory(list);
}
}

Verwandte Interessen