TCS ASPIRE : JAVA ASSIGNMENT NO : 02


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);
}
}



2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete