Você está na página 1de 14

1.

Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent,


switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes
import java.util.*;
class WashingMachine
{
Scanner input=new 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 of clothes:");
int no=input.nextInt();
return no;
}
public void switchOff()
{
System.out.println("The Lid is closed.");
}
public static void main(String args[])
{
WashingMachine wm=new WashingMachine();
wm.switchOn();
int numOfClothes=wm.acceptClothes();
wm.acceptDetergent();
wm.switchOff();
System.out.println(numOfClothes+"clothes get washed");
}
}

2. Create a calculator class which will have methods add, multiply, divide & subtract

import java.util.*;
class Calculation
{
public int add(int a, int b)
{
return a+b;
}
public int subtract(int a, int b)
{
if(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)
{
if(a>b)
{
return a/b;
}
else
{
return b/a;
}
}
}
public class Calculator
{
public static void main(String args[])
{
Calculation cal=new Calculation();
int add=cal.add(5,10);
int sub=cal.subtract(5,10);
int mul=cal.multiply(5,10);
int div=cal.divide(5,10);

System.out.println("Taken Values are 5, 10");


System.out.println("Addition: "+add);
System.out.println("Substraction: "+sub);
System.out.println("Multiplication: "+mul);
System.out.println("Division: "+div);
}
}
3. Create a class called Student which has the following methods:
i. 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.util.*;
public class Student
{
Scanner input=new Scanner(System.in);
public String average()
{
System.out.println("Enter Marks1: ");
double m1=input.nextDouble();
System.out.println("Enter Marks2: ");
double m2=input.nextDouble();
System.out.println("Enter Marks3: ");
double m3=input.nextDouble();
double tm=m1+m2+m3;
double avg=tm/3;
if(avg
{
return "Failed";
}
if(avg>50)
{
return "Passed";
}
return " ";
}
public String getName()
{
System.out.println("Enter Name: ");
String name=input.nextLine();
String result=average();
return name+" get "+result;
}

public static void main(String args[])


{
Student data=new Student();
String nameAndResult=data.getName();
System.out.println(nameAndResult);
}
}
4. 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 javax.swing.*;
class Customer
{
int bal;
Customer(int bal)
{
this.bal=bal;
}
int deposit(int amt)
{
if(amt
{
System.out.println("Invalid Amount");
return 1;
}
bal=bal+amt;
return 0;
}
int withdraw(int amt)
{
if(bal<amt>
{
System.out.println("Not Sufficient balance.");
return 1;
}
if(amt
{

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 Cust=new Customer(1500);
String st1=JOptionPane.showInputDialog(null,"Enter the amount to deposit:");
int dep=Integer.parseInt(st1);
int bal1=Cust.deposit(dep);
Cust.check();
String st2=JOptionPane.showInputDialog(null,"Enter the amount to withdraw:");
int with=Integer.parseInt(st2);
int bal2=Cust.withdraw(with);
Cust.check();
}
}
5. 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.util.*;
class Employee
{
static Scanner input=new Scanner(System.in);
public double netSalary(double salary, double taxrate)
{
double tax=salary*taxrate;
double netpay=salary-tax;
return netpay;
}
public static String grade( )
{
System.out.print("Enter Grade: ");
String grade=input.next();
return grade;
}
public static void main(String[] args)
{

Employee emp=new Employee();


System.out.print("Enter Salary: ");
double sal=input.nextDouble();
System.out.print("Enter Tax in %: ");
double taxrate=input.nextDouble()/100;
String g=emp.grade();
double net=emp.netSalary(sal,taxrate);
System.out.println("Net Salary is: "+net);
System.out.println("Grade is: "+g);
}
}
6. 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.util.*;
class Product
{
int productID;
String name;
int categoryID;
double price;
Product(int productID,String name,int categoryID,double price)
{
this.productID=productID;
this.name=name;
this.categoryID=categoryID;
this.price=price;
}
}
public class ElectricalProduct extends Product
{
int voltageRange;
int wattage;
ElectricalProduct(int productID,String name,int categoryID,double price,int voltageRange,int
wattage)
{
super(productID,name,categoryID,price);

this.voltageRange=voltageRange;
this.wattage=wattage;
}

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 input=new Scanner(System.in);
System.out.println("Enter Product ID: ");
int pid=input.nextInt();
System.out.println("Enter Name: ");
String name=input.next();
System.out.println("Enter Catagory ID: ");
int cid=input.nextInt();
System.out.println("Enter Price: ");
double price=input.nextDouble();
System.out.println("Enter Voltage Range: ");
int vrange=input.nextInt();
System.out.println("Enter Wattage: ");
int wattage=input.nextInt();
System.out.println("****Details of Electrical Product****");
System.out.println();
ElectricalProduct p=new ElectricalProduct(pid,name,cid,price,vrange,wattage);
p.display();
}
}

7. 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.util.*;
class Book

{
int id;
String title;
String author;
double price;
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setTitle(String title)
{
this.title=title;
}
public String getTitle()
{
return title;
}
public void setAuthor(String author)
{
this.author=author;
}
public String getAuthor()
{
return author;
}
public void setPrice(double price)
{
this.price=price;
}
public double getPrice()
{
return price;
}
}
class Periodical
{
String timeperiod;
public void setTimeperiod(String timeperiod)
{
this.timeperiod=timeperiod;
}
public String getTimeperiod()

{
return timeperiod;
}
}
public class BookInformation
{
public static void main(String[]args)
{
Book b=new Book();
Periodical p=new Periodical();
Scanner input=new Scanner(System.in);
System.out.print("Book ID: ");
int id=input.nextInt();
b.setId(id);
System.out.print("Title: ");
String title=input.next();
b.setTitle(title);
System.out.print("Author: ");
String author=input.next();
b.setAuthor(author);
System.out.print("Price: ");
double price=input.nextDouble();
b.setPrice(price);
System.out.print("Period: ");
String pp=input.next();
p.setTimeperiod(pp);
System.out.println();
System.out.println("----Book Information----");
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 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.util.*;
class Book
{
int id;
String title;

String author;
double price;
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setTitle(String title)
{
this.title=title;
}
public String getTitle()
{
return title;
}
public void setAuthor(String author)
{
this.author=author;
}
public String getAuthor()
{
return author;
}
public void setPrice(double price)
{
this.price=price;
}
public double getPrice()
{
return price;
}
}
class Periodical
{
String timeperiod;
public void setTimeperiod(String timeperiod)
{
this.timeperiod=timeperiod;
}
public String getTimeperiod()
{
return timeperiod;
}

}
public class BookInformation
{
public static void main(String[]args)
{
Book b=new Book();
Periodical p=new Periodical();
Scanner input=new Scanner(System.in);
System.out.print("Book ID: ");
int id=input.nextInt();
b.setId(id);
System.out.print("Title: ");
String title=input.next();
b.setTitle(title);
System.out.print("Author: ");
String author=input.next();
b.setAuthor(author);
System.out.print("Price: ");
double price=input.nextDouble();
b.setPrice(price);
System.out.print("Period: ");
String pp=input.next();
p.setTimeperiod(pp);
System.out.println();
System.out.println("----Book Information----");
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());
}
}
9. 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.util.*;
import java.text.*;
class NumberProgram
{
public static void main(String[] args)
{
DecimalFormat df=new DecimalFormat("##.##");
Scanner input=new Scanner(System.in);
System.out.print("Enter Number: ");

double num=input.nextDouble();
System.out.print("Raise Number's power: ");
double pow=input.nextDouble();
double value=Math.pow(num,pow);
System.out.println("Result is: "+df.format(value));
}
}
10. 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.util.*;
class TataMotors
{
String category;
String model;
TataMotors(String category,String model)
{
this.category=category;
this.model=model;
}
public String getCategory()
{
return category;
}
public String getModel()
{
return model;
}
public static void ModelOfCategory(ArrayList<tatamotors> list)</tatamotors>
{
Scanner input=new Scanner(System.in);
System.out.print("Enter Category: (SUV/SEDAN/ECONOMY/MINI)");
String category=input.nextLine();
System.out.println();
System.out.print("Model is: ");
for (TataMotors tm : list)
{
if(tm.getCategory().equals(category))
{
System.out.print(tm.getModel());
}
}

}
public static void main(String[] args)
{
ArrayList<tatamotors> list=new ArrayList<tatamotors>();</tatamotors></tatamotors>
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"));
ModelOfCategory(list);
}
}

EXCEPTION HANDLING:
Core Java Assignment - II
1. Write a program to demonstrate the use of try, catch, finally throw and throws keywords and
demonstrate the following points in the program.
a) Multiple catch blocks.
b) try-catch-finally combination.
c) try-finally combination.
d) Exception propagation among many methods.
e) Use of getMessage(), printStackTrace() function of Throwable class.
f) Nested try blocks
2. Write a program to throw a checked exception explicitly using 'throw' keyword and
a) Handle the exception in same method.
b) use throws clause and handle the exception in some other method (calling method)
c) Don't either handle or use the throws clause.
3. Repeat program 2 with unchecked Exception and demonstrate the difference in both program.
4. Create a user defined exception to check whether your employee exist in your data structure (use any
data structure to store the employees - like array, ArrayList etc) and throw exception if name is not in
the employees list. Use the catch and finally block to make an appropriat

CASE STUDY
Monopoly bank offers various fixed deposit schemes. Customers of the bank can choose to deposit
money under various schemes. For every scheme there is a minimum amount of money that should be
deposited. An interest is paid for the deposit amount as mentioned in the scheme under which deposit

is made. Every scheme states a time period for which the money is deposited. A penalty amount as
specified by the scheme needs to be paid if the money needs to be withdrawn before the mentioned
period. In this case interest will be paid only for the time period for which money was deposited. Write a
program to:
a. Add a new scheme
b. Add a new customer
c. Open a deposit for a customer
d. Calculate amount payable for a deposit, at the end of deposit period
e. Calculate amount payable for a deposit, if the money is withdrawn before specified period

Inheritance classes:
1.Sell Now manages an online advertisement site, where users can advertise about various products
they want to sell. Users can post advertisements to sell various products. Users need to provide the
category of the product (e.g Electronics, Books, Textiles, and Vehicle etc), brand of the product (e.g
Nokia, Reebok, etc.). User can anytime remove the advertisement. Write a program to
a.Register a user
b.Post an advertisement
c.Remove an advertisement
2. 1.All Smarts university has a library with extensive collection of books. A student can rent up to 5
books at a time. Write a program to manage:
a.Issuing books
b.Returning books
c.Searching for book using title
3. 1.Keep Talking is a leading mobile communication service provider in the country. They offer postpaid mobile connection services across cities. There are various tariff plans they offer to customers.
Customers calls are charged based on the plan they have opted for. Write a program to:
a.Calculate bill of a customer
b.Find number of customer who have opted for a particular tariff plan
c.Find the connection with the highest bill

Você também pode gostar