Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
99 views

Assignment - 2: Solution 1

The document contains 8 solutions that define Java classes to model different real world entities like a washing machine, calculator, student, bank account, employee, electrical product, book, and vehicle. Each solution defines the necessary attributes and behaviors of the class using methods. Main method is used to create objects of the class and call methods to demonstrate functionality.

Uploaded by

Saif Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Assignment - 2: Solution 1

The document contains 8 solutions that define Java classes to model different real world entities like a washing machine, calculator, student, bank account, employee, electrical product, book, and vehicle. Each solution defines the necessary attributes and behaviors of the class using methods. Main method is used to create objects of the class and call methods to demonstrate functionality.

Uploaded by

Saif Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment – 2

Solution 1.

class  WashingMachine
{
       public void switchOn()
{
        System.out.println ("The indicator is on and the lid is open.");
   }

    public int acceptClothes()


{
   System.out.println("Please enter the no. of clothes: ");
    int no=Interger.parseInt(args[0]);
   return no;
   }
    public void start()
{
        System.out.println ("Start washing cloths.");
    }

    public void acceptDetergent()


{
        System.out.println("Detergent is adding ");
        start();
       }   
public void switchOff()
{
        System.out.println ("Indicator off and the lid is closed.");
   }

    public static void main(String args[])


  {
        WashingMachine wm=new WashingMachine();
        wm.switchOn();
        int no_of_cloths=wm.acceptClothes();
        wm.acceptDetergent();
        wm.switchOff();
        System.out.println(no_of_cloths +" clothes get washed");
}
}
Solution 2.

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

public double divide(double a,double b)


{       
double div=a / b;
return div;
   }

public double substract(double a,double b)


{       
double sub=a - b;
return sub;
    }   

    public static void main(String args[])


    {       
System.out.println ("Enter 1 for Addition :");
System.out.println ("Enter 2 for Substraction :");
System.out.println ("Enter 3 for Multiplication :");
System.out.println ("Enter 4 for Division :");
int x =Integer.parseInt(args[0]);
System.out.println ("Please enter the first number :");
double a =Double.parseDouble(args[1]);
System.out.println ("Please enter the second number :");
double b =Double.parseDouble(args[2]);
Calculator cal=new Calculator();
if(x==1)
{
double result=cal.add(a,b);
}
else if(x==2)
{
double result=cal.substract(a,b);
}
else if(x==3)
{
double result=cal.multi(a,b);
}
else if(x==4)
{
double result=cal.divide(a,b);
}
else
{
System.out.println(“Invalid Choice”);
}
}
}
Solution 3.

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

public string getName()


{
System.out.println("Please enter the Student Name:");
string  name=args[3];
string result=average();
return name+" has "+result;
}
public static void main(String args[])
{
Student objstud=new Student();
string information=objstud.getName();
System.out.println(information);
}
}
Solution 4.

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

public static void main(String args[])


{
Bank objbank=new Bank();
System.out.Println(“Please press 1 for deposit and 2 for withdraw”);
int i=Integer.parseInt(args[0]);
if(i==1)
{
System.out.Println(“Please enter the amount of deposit :”);
double amt= double.parsedouble (args[1]);
System.out.Println(“Please enter the your balance :”);
double bal= double.parsedouble (args[2]);
double newbal=objbank.deposit(amt,bal);
System.out.Println(“Your amount is deposited successfully and your new balance is: ”+
newbal);

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

public string getGrade()


{
System.out.println("Please enter the Employee Grade:");
string  grade=args[0];
return grade;
}
public static void main(String args[])
{
Employee objemp=new Employee();
string grad=objemp.getGrade();
System.out.Println(“Please enter the salary :”);
double sal=double.parsedouble(args[1]);
System.out.Println(“Please enter the tax :”);
double tx= double.parsedouble (args[2]);
double net=objemp.netSalary(sal,tx);
System.out.println(“Your employee grade is :” + grad + “ and your Net Salary is :” + net);
}
}
Solution 6.

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;

    Vehicle(int no,String model,String manufacturer,String color){


        this.no=no;
        this.model=model;
        this.manufacturer=manufacturer;
        this.color=color;
  }
}
    public class Truck extends Vehicle{
        int capacity;
        Truck(int no,String model,String manufacturer,String color,int capacity){
        super( no, model, manufacturer, color);
        this.capacity=capacity;
    }
    void show(){
      System.out.println("No = " + no);
      System.out.println("Model = " + model);
      System.out.println("manufacturer = " + manufacturer);
      System.out.println("Color = " + color);
      System.out.println("Capacity = " + capacity);
  }

    public static void main(String[] args)


  {
       System.out.println("Truck No: ");
    int no=Integer.parseInt(args[0]);
    System.out.println("Model: ");
    String model=args[1];

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

        Truck objt=new Truck(no,model,manufacturer,color,cap);


        System.out.println("Truck Details");
        System.out.println();
        obj.show();
}
}

Solution 9.
class Number
{

public static void main(String args[])


{
double result=1.0;
System.out.Println(“Please enter Number :”);
double no=double.parsedouble(args[0]);
System.out.Println(“Please enter the Power :”);
int pow= Integer.parseInt (args[1]);
for(int i=1;i<=pow;i++)
{
result=result*no;
}
System.out.println(“Result is : “ + result);
}
}

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

    public static void main(String[] args)


  {
        ArrayList<TataMotors> list=new ArrayList<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);
 }
}

You might also like