Professional Documents
Culture Documents
Assignment 2
Assignment 2
import java.util.*;
class WashingMachine
{
Scanner input=new Scanner(System.in);
public void switchOn () {
System.out.println ("The lid is open.");
}
2. Create a calculator class which will have methods add, multiply, divide & subtract
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(add);
System.out.println(sub);
System.out.println(mul);
System.out.println(div);
}
}
ii. Inputname: which would accept the name of the student & returns the name.
import java.util.*;
Customer(int bal) {
this.bal = bal;
}
int deposit(int amt) {
if (amt < 0) {
System.out.println("Invalid Amount");
return 1;
}
bal = bal + amt;
return 0;
}
}
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;
this.productID=productID;
this.name=name;
this.categoryID=categoryID;
this.price=price;
int voltageRange;
int wattage;
super(productID,name,categoryID,price);
this.voltageRange=voltageRange;
this.wattage=wattage;
System.out.println("Name: "+name);
System.out.println("Price: "+price);
System.out.println("Wattage: "+wattage);
int pid=input.nextInt();
String name=input.next();
int cid=input.nextInt();
double price=input.nextDouble();
int vrange=input.nextInt();
int wattage=input.nextInt();
System.out.println();
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;
this.id=id;
return id;
this.title=title;
this.author=author;
return author;
this.price=price;
return price;
class Periodical
String timeperiod;
this.timeperiod=timeperiod;
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("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 Vehicle
int no;
String model;
String manufacturer;
String color;
this.no=no;
this.model=model;
this.manufacturer=manufacturer;
this.color=color;
}
}
int capacity;
this.capacity=capacity;
void show(){
int no=input.nextInt();
System.out.println("Model: ");
String model=input.next();
System.out.println("Manufacturer: ");
String manufacturer=input.next();
System.out.println("Color: ");
String color=input.next();
int cap=input.nextInt();
System.out.println("****Truck Details****");
System.out.println();
t.show();
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
double num=input.nextDouble();
System.out.print("Raise Number's power: ");
double pow=input.nextDouble();
double value=Math.pow(num,pow);
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;
this.category=category;
this.model=model;
return category;
return model;
String category=input.nextLine();
System.out.println();
if(tm.getCategory().equals(category)){
System.out.print(tm.getModel());
ModelOfCategory(list);