Assignment - 2: Solution 1
Assignment - 2: Solution 1
Solution 1.
class WashingMachine
{
public void switchOn()
{
System.out.println ("The indicator is on and the lid is open.");
}
class Calculator
{
public double add(double a,double b)
{
double sum=a + b;
return sum;
}
public double multiply(double a,double b)
{
double multi=a * b;
return multi;
}
class Student
{
public string averageMarks()
{
System.out.println("Please enter Marks of first Examination: ");
double m1=Double.parseDouble(args[0]);
System.out.println("Please enter Marks of second Examination: ");
double m2=Double.parseDouble(args[1]);
System.out.println("Please enter Marks of third Examination: ");
double m3=Double.parseDouble(args[2]);
double total=m1+m2+m3;
double avg=total/3;
if(avg>=50)
{
return “Passed”;
}
else
{
return "Failed";
}
}
class Bank
{
public double deposit(double amount, double balance)
{
double newBalance=amount+balance;
return newbalance;
}
public double withdraw(double amount, double balance)
{
if(balance>=amount)
{
double newBalance=balance - amount;
return newbalance;
}
else
{
Return 0;
}
}
}
else if(i==2)
{
System.out.Println(“Please enter the amount to withdraw :”);
double amt=double.parsedouble(args[1]);
System.out.Println(“Please enter the your balance :”);
double bal= double.parsedouble (args[2]);
double newbal=objbank.withdraw(amt,bal);
System.out.Println(“your new balance is: ”+ newbal);
}
else
{
System.out.Println(“Invalid choice.”);
}
}
}
Solution 5.
class Employee
{
public double netSalary(double salary,double tax)
{
double netsal=salary-tax;
return netsal;
}
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)
{
System.out.println("Enter Product ID: ");
int pid=Integer.parseInt(args[0]);
System.out.println("Enter Name: ");
string name=args[1];
System.out.println("Enter Catagory ID: ");
int cid=Integer.parseInt(args[2]);
System.out.println("Enter Price: ");
double price=Double.parseDouble(args[3]);
System.out.println("Enter Voltage Range: ");
int vrange= Integer.parseInt(args[4]);
System.out.println("Enter Wattage: ");
int wattage= Integer.parseInt(args[5]);
System.out.println("Details of Electrical Product ");
System.out.println();
ElectricalProduct obj=new ElectricalProduct(pid,name,cid,price,vrange,wattage);
obj.display();
}
}
Solution 7.
class Book{
int bookID;
string title;
string author;
double price;
Book(int bookID,string title,string author,double price){
this.bookID=bookID;
this.title=title;
this.author=author;
this.price=price;
}
}
public class Periodical extends Book{
string period;
Periodical(int bookID,string title,string author,double price,string period){
super(bookID,title,author,price);
this.period=period;
}
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)
{
System.out.println("Enter book ID: ");
int bid=Integer.parseInt(args[0]);
System.out.println("Enter Title: ");
string title=args[1];
System.out.println("Enter Author Name: ");
int author=Integer.parseInt(args[2]);
System.out.println("Enter Price: ");
double price=Double.parseDouble(args[3]);
System.out.println("Enter Period: ");
int period= Integer.parseInt(args[4]);
System.out.println("Details of Book ");
System.out.println();
Periodical obj=new Periodical(bid,title,author,price,period);
obj.display();
}
}
Solution 8.
class Vehicle
{
int no;
String model;
String manufacturer;
String color;
System.out.println("Manufacturer: ");
String manufacturer=args[2];
System.out.println("Color: ");
String color=args[3];
System.out.println("Loading Capacity: ");
int cap=Integer.parseInt(args[4]);
Solution 9.
class Number
{
Solution 10.
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){
System.out.print("Enter Category: ");
string category=Integer.parseInt(args[0]);
System.out.println();
System.out.print("Model is: ");
for (TataMotors tm : list){
if(tm.getCategory().equals(category)){
System.out.print(tm.getModel());
}
}
}
ModelOfCategory(list);
}
}