Search This Blog

Popular Posts

Saturday, December 24, 2011

Aspire – Java Assignment-2 - Solutions

1. Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent, switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes
Solution:-
import java.util.*;
class Washingmachine
{
Scanner s=new Scanner(System.in);
public void switchOn()
{
System.out.println("Switched on");
}
public void start()
{
System.out.println("Machine started");
}
public void acceptDetergent()
{
System.out.println("Accept detergent");
start();
}
public int acceptClothes()
{
System.out.println("Enter number of clothes::");
return (s.nextInt());
}
public void switchOff()
{
System.out.println("Machine switched OFF");
}
public static void main(String args[])
{
Washingmachine ob=new Washingmachine();
ob.switchOn();
int n=ob.acceptClothes();
ob.acceptDetergent();
ob.switchOff();
System.out.println("the number of clothes washed are ::"+n);
}
}
------------------------------------------------------------------------------------------------------------
2. Create a calculator class which will have methods add, multiply, divide & subtract
Solution:-
import java.util.*;
public class Calculator
{
public double add(double a, double b)
{
return a+b;
}
public double subtract(double a, double b)
{
return a-b;
}
public double multiply(double a, double b)
{
return a*b;
}
public double divide(double a, double b)
{
return a/b;
}



public static void main(String args[])
{
Calculator ob=new Calculator();
Scanner s=new Scanner(System.in);
System.out.println("Enter the choice for the following computation to be performed :-");
System.out.println("1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division");
int ch=s.nextInt();
System.out.print("Enter the two operands ::");
int a=s.nextInt();
int b=s.nextInt();
switch (ch)
{
case 1:
System.out.println("The sum of "+a+"+"+b+" = "+ob.add(a,b));
break;
case 2:
System.out.println("The subtraction of "+a+"-"+b+" = "+ob.subtract(a,b));
break;
case 3:
System.out.println("The multiplication of "+a+"x"+b+" = "+ob.multiply(a,b));
break;
case 4:
System.out.println("The division of "+a+"/"+b+" = "+ob.divide(a,b));
break;
default:
System.out.println("Wrong choice!!!");
}

}
}
------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.util.*;
public class Student
{
Scanner s=new Scanner(System.in);
public String Average()
{
System.out.println("Enter Marks1: ");
double m1=s.nextDouble();
System.out.println("Enter Marks2: ");
double m2=s.nextDouble();
System.out.println("Enter Marks3: ");
double m3=s.nextDouble();
double tm=m1+m2+m3;
double avg=tm/3;
if(avg<50)
{
return "Failed";
}
if(avg>50)
{
return "Passed";
}
return " ";
}
public String Inputname()
{
System.out.println("Enter Name: ");
String name=s.nextLine();
return Average();
}
public static void main(String args[])
{
Student ob=new Student();
System.out.println(ob.Inputname());
}
}
------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.util.*;
public class Bank
{
int bal;
Bank(int bal)
{
this.bal=bal;
}
int deposit(int amt)
{
if(amt<0)
{
System.out.println("Invalid Amount");
return 1;
}
System.out.println("Money deposited !!!");
return (bal+amt);

}
int withdraw(int amt)
{
if(bal {
System.out.println("Not Sufficient balance.");
return 0;
}
if(amt<0)
{
System.out.println("Invalid Amount");
return 0 ;
}
bal=bal-amt;
System.out.println("Withdrawal done !!!");
return bal;
}

public static void main(String args[])
{
Scanner s=new Scanner(System.in);
Bank ob=new Bank(1000);
System.out.print("Enter 1.for Deposit \n 2.for Withdrawal :: ");
int ch=s.nextInt();
System.out.println("Enter the amount ::");
int amt=s.nextInt();
if(ch==1)
{
System.out.println("The new balance is :: "+ob.deposit(amt));
}
else
{
int ret=ob.withdraw(amt);
if(ret>0)
{
System.out.println("The new balance is :: "+ret);
}
}
}
}
------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.util.*;
class Employee
{
static Scanner s=new Scanner(System.in);
public double netSalary(double salary, double tax)
{
return (salary-tax);

}
public static String grade( )
{
System.out.print("Enter Grade: ");
return (s.next());
}
public static void main(String[] args)
{
Employee ob=new Employee();
System.out.print("Enter Salary: ");
double sal=s.nextDouble();
System.out.print("Enter Tax : ");
double tax=s.nextDouble();
String g=ob.grade();
double net=ob.netSalary(sal,tax);
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.
Solution:-
import java.lang.*;
import java.util.*;
class Product
{
int productid;
String name;
int categoryid;
double unitprice;
public Product(int pid,String naam,int catid,double uniprice)
{
this.productid=pid;
this.name=naam;
this.categoryid=catid;
this.unitprice=uniprice;
}
}

class Electricalproduct extends Product
{
int voltagerange;
int wattage;
static Scanner s=new Scanner(System.in);

public Electricalproduct(int pid,String naam,int catid,double uniprice,int voltrange,int watt)
{
super(pid,naam,catid,uniprice);
this.voltagerange=voltrange;
this.wattage=watt;
}
public void display()
{
System.out.println("Product ID: "+productid);
System.out.println("Name: "+name);
System.out.println("Category ID: "+categoryid);
System.out.println("Price: "+unitprice);
System.out.println("Voltage Range: "+voltagerange);
System.out.println("Wattage: "+wattage);
}
public void behaviour()
{

System.out.print("Enter the new wattage::");
this.wattage=s.nextInt();
System.out.print("Enter new price::");
double price=s.nextDouble();

super.unitprice=price;
display();
}
public static void main(String []args)
{

System.out.println("Enter Product ID: ");
int pid=s.nextInt();
System.out.println("Enter Name: ");
String name=s.next();
System.out.println("Enter Catagory ID: ");
int cid=s.nextInt();
System.out.println("Enter Price: ");
double price=s.nextDouble();
System.out.println("Enter Voltage Range: ");
int vrange=s.nextInt();
System.out.println("Enter Wattage: ");
int wattage=s.nextInt();
System.out.println("****Details of Electrical Product****");
System.out.println();
Electricalproduct p=new Electricalproduct(pid,name,cid,price,vrange,wattage);
p.display();
System.out.println("*************************************************************");
System.out.println("Would u like to reenter the price and wattage(Press 1.for yes or 2 for No)::");
int ch=s.nextInt();
if(ch==1)
{p.behaviour();}
}
}

------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.lang.*;
import java.util.*;
class Book
{
int bookid;
String title;
String author;
double price;
public Book(int bid,String tit,String auth,double price)
{
this.bookid=bid;
this.title=tit;
this.author=auth;
this.price=price;
}
}

class Periodical extends Book
{
String period;
static Scanner s=new Scanner(System.in);
public Periodical(int bid,String tit,String auth,double price,String period)
{
super(bid,tit,auth,price);
this.period=period;
}
public void display()
{
System.out.println("Book ID: "+bookid);
System.out.println("Title: "+title);
System.out.println("Author: "+author);
System.out.println("Price: "+price);
System.out.println("Period: "+period);
}
public void behaviour()
{

System.out.print("Enter the new period::");
this.period=s.next();
System.out.print("Enter new price::");
double price=s.nextDouble();

super.price=price;
display();
}
public static void main(String []args)
{

System.out.println("Enter Book ID: ");
int bid=s.nextInt();
System.out.println("Enter Title: ");
String tit=s.next();
System.out.println("Enter Author: ");
String auth=s.next();
System.out.println("Enter Price: ");
double price=s.nextDouble();
System.out.println("Enter Period: ");
String period=s.next();
System.out.println("*****Details*****");
Periodical p=new Periodical(bid,tit,auth,price,period);
p.display();
System.out.println("*************************************************************");
System.out.println("Would u like to reenter the price and Period(Press 1.for yes or 2 for No)::");
int ch=s.nextInt();
if(ch==1)
{p.behaviour();}
}
}
------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.lang.*;
import java.util.*;
class Vehicle
{
int vehicleno;
String model;
String manufacturer;
String color;
public Vehicle(int vno,String mod,String manu,String colr)
{
this.vehicleno=vno;
this.model=mod;
this.manufacturer=manu;
this.color=colr;
}
}

class Truck extends Vehicle
{
int loadingcapacity;
static Scanner s=new Scanner(System.in);
public Truck(int vno,String mod,String manu,String colr,int loadcap)
{
super(vno,mod,manu,colr);
this.loadingcapacity=loadcap;
}
public void display()
{
System.out.println("Vehicle number :: "+vehicleno);
System.out.println("Model :: "+model);
System.out.println("Manufacturer :: "+manufacturer);
System.out.println("Color :: "+color);
System.out.println("Loading Capacity:: "+loadingcapacity);
}
public void behaviour()
{

System.out.print("Enter the new color::");
super.color=s.next();
System.out.print("Enter new loading capacity::");
int ldcap=s.nextInt();

this.loadingcapacity=ldcap;
display();
}
public static void main(String []args)
{

System.out.println("Enter Vehicle No: ");
int vno=s.nextInt();
System.out.println("Enter Model: ");
String mod=s.next();
System.out.println("Enter Manufacturer: ");
String manu=s.next();
System.out.println("Enter Color: ");
String col=s.next();
System.out.println("Enter Loading Capacity: ");
int ldcap=s.nextInt();
System.out.println("*****Details*****");
Truck p=new Truck(vno,mod,manu,col,ldcap);
p.display();
System.out.println("*************************************************************");
System.out.println("Would u like to reenter the color and loading capacity(Press 1.for yes or 2 for No)::");
int ch=s.nextInt();
if(ch==1)
{p.behaviour();}
}
}
------------------------------------------------------------------------------------------------------------
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.
Solution:-
import java.util.*;
import java.text.*;
class Powerprg
{
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("Number raised to 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.
Solution:-
import java.util.*;
class Tata
{
String category;
String model;
Tata(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 list)
{
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 (Tata tm : list)
{
if(tm.getCategory().equals(category))
{
System.out.print(tm.getModel());
}
}
}
public static void main(String[] args)
{
ArrayList list=new ArrayList();
list.add(new Tata("SUV","TATA SAFARI"));
list.add(new Tata("SEDAN","TATA INDIGO"));
list.add(new Tata("ECONOMY","TATA INDICA"));
list.add(new Tata("MINI","TATA NANO"));
ModelOfCategory(list);
}
}


------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment