Assignment - II Basic Java Programming

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

Assignment –II

Basic java programming


DT20100869623

Program – (1): Create a washing machine class with methods as switch On, accept
Clothes, accept Detergent, and switch Off. Accept Clothes & accepts the no of
Clothes as argument & returns the no of Clothes

Sol:

import java.util.Scanner;
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();

1
        wm.switchOn();
       int numOFClothes = wm.acceptClothes();
       wm.acceptDetergent();
       wm.switchOff();
           System.out.println(numOFClothes+" clothes get washed");
   }
}

Program – (2) Create calculator classes which will have methods add, multiply,
divide & subtract):

Sol: 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){

2
Scanner input = new Scanner(System.in);
        Calculation cal=new Calculation ();

int a ,b ;

System.out.println (“Enter the value of a”);

a=in.nextInt ();

System.out.println (“Enter the value of b”);

b=in.nextInt ();
    int add=cal.add(a,b);
        int sub=cal.subtract(a,b);
   int mul=cal.multiply(a,b);
         int div=cal.divide(a,b);

System.out.println(“Calculator in the order ‘+,-,*,/’ is”); System.out.print(add);


System.out.print(sub);
      System.out.print(mul);
    System.out.print(div);
  }
}

Program – (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. In put name: which would accept the name of the student & returns the name?

Sol: import java.util.Scanner;


public class Student{
Scanner input=new Scanner(System.in);
public String average(){
System.out.print("Enter Marks1: ");
double m1=input.nextDouble();
System.out.print("Enter Marks2: ");

3
double m2=input.nextDouble();
System.out.print("Enter Marks3: ");
double m3=input.nextDouble();
double tm=m1+m2+m3;
double avg=tm/3;
if(avg<50){
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 nameAndResut=data.getName();
System.out.println(nameAndResut);
}

Program – (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.

Sol:

import javax.swing.*;
class  Customer{
        int bal;

4
        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;
    }
        int withdraw(int amt) {
                if (bal < amt) {
                        System.out.println("Not sufficient balance.");
                        return 1;                }
                if (amt < 0) {
                        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();

5
        String st2=JOptionPane.showInputDialog(null, "Enter the amount to withdraw");
        int with=Integer.parseInt(st2);
        int bal2=Cust.withdraw(with);
        Cust.check();
    }
  }

Program – (5) Create an Employee class which has methods net Salary which would
accept salary & tax as arguments & returns the net Salary which is tax deducted from
the salary. Also it has a method grade which would accept the grade of the employee
& return grade.

Sol: import java.util.*;


class  Employee{
    public double netSalary(double salary, double taxrate){
    double tax=salary*taxrate;
    double netpay=salary+tax;
int grade;
    return netpay;
}
    public static void main(String[] args)     {
        Employee emp=new Employee();
        Scanner input=new Scanner(System.in);
        System.out.print("Enter Salary: ");
    double sal=input.nextDouble();
System.out.print(“Enter grade of the employee”);
int grade=input.nextInt();
        System.out.print("Enter Tax in %: ");
        double taxrate=input.nextDouble()/100;
        double net=emp.netSalary(sal,taxrate);
        System.out.println("Net Salary is: "+net);
System.out.println(“Grade is :”+grade);
  }
}

6
Program - (6) Create Product having following attributes: Product ID, Name,
Category ID and Unit Price. Create Electrical Product having the following additional
attributes: Voltage Range and Wattage. Add a behavior to change the Wattage and
price of the electrical product. Display the updated Electrical Product details.

Sol:
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);

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

Program-(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.

Sol: import java.util.*;


 class Book{
    int id;
    String title;
    String author;

8
    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(){

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

10
Program-(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.

Sol: import java.util.*;


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)

11
  {
    Scanner input=new Scanner(System.in);
    System.out.println("Truck No: ");
    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();
    System.out.println("Loading Capacity: ");
    int capacity=input.nextInt();
        Truck t=new Truck(no, model ,manufacturer ,color ,capacity);
        System.out.println("****Truck Details****");
        System.out.println();
        t.show();

System.out.println (“enter the new color of the truck”);

String t.color= input.nextInt ();


System.out.println (“enter the new value of the capacity”);

int t.capacity= input.nextInt ();

System.out.println (“The New updated values of the truck are”);

t.show ();
  }
}

Program-(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.

Sol: import java.util.*;


import java.text.*;
class  NumberProgram

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

Program-(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",

Sol:

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){

13
        Scanner input=new Scanner(System.in);
        System.out.print("Enter Category: ");
        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>();
        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);
  }

14

You might also like