Constructor

 1.      Design a class named ShowRoom with the following description:

 

Instance variables/data members:

 

String name: to store the name of the customer.

long mobno: to store the mobile number of the customer.

double cost: to store the cost of the items purchased.

double dis: to store the discount amount.

double amount: to store the amount to be paid after discount.

 

Member methods:

 

ShowRoom(): default constructor to initialize data members.

void input(): to input customer name, mobile number, cost.

void calculate(): to calculate discount on the cost of purchased items, based on the following criteria:

 

COST                                           DISCOUNT (IN PERCENTAGE)

 

Less than or equal to Rs. 10000                           5%

More than Rs. 10000 and less than or equal to Rs. 20000   10%

More than Rs. 20000 and less than or equal to Rs. 35000   15%

More than Rs. 35000                                       20%

 

void display(): to display customer name, mobile number, amount to be paid after discount.

 

Write a main() method to create an object of the class and call the above member methods.

 

 

SOLUTION

 

import java.util.Scanner;

class ShowRoom{

  //as mentioned in the question

     String name;

     long mobno;

     double cost;

     double dis;

     double amount;

    public ShowRoom(){

        name = "";

        mobno = 0l;

        cost = 0.0;

        dis = 0.0;

        amount = 0.0;

    }

    public void input(){

       Scanner sc = new Scanner(System.in);

        System.out.print("Please enter Customer name: ");

        name = sc.nextLine();

        System.out.print("Please enter Mobile Number: ");

        mobno = sc.nextLong();

        System.out.print("Please enter Cost: ");

        cost = sc.nextDouble();

    }

    public void calculate(){

        if(cost <= 10000)

            dis = 5.0/100.0 *cost;

        else if(cost <= 20000)

            dis = 10.0/100.0*cost;

        else if(cost <= 35000)

            dis = 15.0/100.0 *cost;

        else

        dis = 20.0 / 100.0 * cost;

        amount = cost - dis;

    }

    public void display(){

        System.out.println("Customer name is: " + name);

        System.out.println("Mobile Number is: " + mobno);

        System.out.println("Total Amount is: " + amount);

    }

    public static void main(String args[]){

        ShowRoom obj = new ShowRoom();

        obj.input();

        obj.calculate();

        obj.display();

    }

}

 

 

2.      Design a class RailwayTicket with the following description:

 

Instance variables/data members:

 

String name: to store the name of the customer.

String coach: to store the type of coach customer wants to travel.

long mobno: to store customer’s mobile number.

int amt: to store basic amount of ticket.

int totalamt: to store the amount to be paid after updating the original amount.

 

Methods:

 

void accept(): to take input for name, coach, mobile number and amount.

void update(): to update the amount as per the coach selected. Extra amount to be added in the amount as follows:

 

Type of coaches                     Amount

 

First_AC                                 700

Second_AC                             500

Third_AC                               250

sleeper                                     None

 

void display(): To display all details of a customer such as name, coach, total amount and mobile number.

Write a main() method to create an object of the class and call the above methods.

 

SOLUTION

 

import java.util.Scanner;

 

class RailwayTicket

{

    String name;

    String coach;

    long mobno;

    int amt;

    int totalamt;

 

    void accept()

    {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please Enter name: ");

        name = sc.nextLine();

        System.out.print("Please Enter coach: ");

        System.out.println(" First_AC, Second_AC, Third_AC or sleeper");

        coach = sc.nextLine();

        System.out.print("Please Enter mobno: ");

        mobno = sc.nextLong();

        System.out.print("Please Enter amt: ");

        amt = sc.nextInt();

    }

 

    void update()

    {

        if (coach.equalsIgnoreCase("First_AC"))

           totalamt = amt + 700;   

        else if (coach. equalsIgnoreCase ("Second_AC"))

             totalamt = amt + 500; 

          else if (coach. equalsIgnoreCase ("Third_AC"))

            totalamt = amt + 250;

         else if (coach. equalsIgnoreCase ("sleeper"))

            totalamt = amt;

    }

 

    void display() {

    System.out.println("Details of customer");

        System.out.println("Name: " + name);

        System.out.println("Coach: " + coach);

        System.out.println("Mobile Number: " + mobno);

        System.out.println("Amount: " + amt);

        System.out.println("Total Amount: " + totalamt);

    }

 

    public static void main(String args[]) {

        RailwayTicket obj = new RailwayTicket();

        obj.accept();

        obj.update();

        obj.display();

    }

}

 

 

3.      Define a class Electric Bill with the following specifications:

 

Instance Variable/ data member:

 

String n – to store the name of the customer

int units – to store the number of units consumed

double bill – to store the amount to paid

 

Member methods:

 

Void accept() – to accept the name of the customer and number of units consumed

Void calculate() – to calculate the bill as per the following tariff :

 

Number of units         Rate per unit

 

First 100 units         Rs.2.00

Next 200 units          Rs.3.00

Above 300 units         Rs.5.00

A surcharge of 2.5% charged if the number of units consumed is above 300 units.

Void print() – To print the details as follows :

Name of the customer ………

Number of units consumed ……

Bill amount …….

 

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

 

import java.util.Scanner;

 

class ElectricBill

{

   String n;

   int units;

   double bill;

 

  public void accept()

  {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter name: ");

    n = sc.next();

    System.out.print("Please Enter units: ");

    units = sc.nextInt();

  }

 

  public void calculate()

  {

    if (units <= 100)

    {

      bill = units * 2;

    }

    else if (units > 100 && units <= 300)

    {

      bill = 100 * 2 + (units - 100) * 3;

    }

    else

    {

      bill = 100 * 2 + 200 * 3 + (units - 300) * 5;

      double surcharge = bill * 2.5 / 100;

      bill = bill + surcharge;

    }

  }

 

  public void print() {

    System.out.println("Name of the customer is" + n);

    System.out.println("Number of units consumed is " + units);

    System.out.println("Bill amount is " + bill);

  }

 

  public static void main(String[] args) {

    ElectricBill obj = new ElectricBill();

    obj.accept();

    obj.calculate();

    obj.print();

  }

}

        

 

4.      Define a class named BookFair with the following description:

 

Instance variables/Data members:

 

String Bname – stores the name of the book.

double price – stores the price of the book.

 

Member Methods:

 

(i) BookFair() – Default constructor to initialize data members.

(ii) void Input() – To input and store the name and the price of the book.

(iii) void calculate() – To calculate the price after discount. Discount is calculated based on the following criteria.

 

PRICE                                                                                                  DISCOUNT

 

Less than or equal to Rs 1000                                                             2% of price

More than Rs 1000 and less than or equal to Rs 3000                     10% of price

More than Rs 3000                                                                              15% of price

 

(iv) void display() – To display the name and price of the book after discount.

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

 

import java.util.Scanner;

 

 class BookFair

{

  String Bname;

  double price;

 

  public BookFair() {

    Bname = "";

    price = 0.0;

  }

 

  public void Input()

  {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter book name: ");

    Bname = sc.nextLine();

    System.out.print("Please Enter price: ");

    price = sc.nextDouble();

  }

 

  void calculate()

  {

    double discountPer= 0;

    if (price <= 1000) {

      discountPer = 2;

    } else if (price > 1000 && price <= 3000) {

      discountPer = 10;

    } else if (price > 3000) {

      discountPer = 15;

    }

    price = price - (price * discountPer / 100);

  }

 

   void display()

  {

    System.out.println("Name is: " + Bname);

    System.out.println("Price after discount is: " + price);

  }

 

  public static void main(String[] args)

  {

    BookFair obj = new BookFair();

    obj.Input();

    obj.calculate();

    obj.display();

  }

}

 

 

5.      Define a class ParkingLot with the following description:

 

Instance variables/data members:

 

int vno – To store the vehicle number

int hours – To store the number of hours the vehicle is parked in the parking lot

double bill – To store the bill amount

 

Member methods:

 

void input() – To input and store vno and hours

void calculate() – To compute the parking charge at the rate of Rs.3 for the first hour or part thereof,

and Rs.1.50 for each additional hour or part thereof.

void display() – To display the detail

Write a main method to create an object of the class and call the above methods

 

SOLUTION

 

import java.util.Scanner;

 

class ParkingLot

{

  int vno;

  int hours;

  double bill;

 

  public void input() {

    Scanner sc = new Scanner(System.in);

    System.out.print("Please Enter vehicle number: ");

    vno = sc.nextInt();

    System.out.print("Please Enter hours: ");

    hours = sc.nextInt();

  }

 

  public void calculate()

  {

    bill = 3 + (hours - 1) * 1.50;

  }

 

  public void display() {

    System.out.println("Vehicle number is: " + vno);

    System.out.println("Hours is: " + hours);

    System.out.println("Bill is: Rs. " + bill);

  }

 

  public static void main(String[] args) {

    ParkingLot obj = new ParkingLot();

    obj.input();

    obj.calculate();

    obj.display();

  }

}

 

 

6.      Define a class named movieMagic with the following description:

 

Instance variables/data members:

int year – to store the year of release of a movie

String title – to store the title of the movie.

float rating – to store the popularity rating of the movie.

(minimum rating = 0.0 and maximum rating = 5.0)

 

Member Methods:

 

(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to "".

(ii) void accept() To input and store year, title and rating.

(iii) void display() To display the title of a movie and a message based on the rating as per the table below.

 

RATING                                                MESSAGE TO BE DISPLAYED

 

0.0 to 2.0                                  Flop

2.1 to 3.4                                  Semi-hit

3.5 to 4.5                                  Hit

4.6 to 5.0                                  Super Hit

 

 

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

 

import java.util.Scanner;

 

class movieMagic {

 

    int year;

    String title;

    float rating;

 

    public movieMagic() {

        year = 0;

        title = "";

        rating = 0;

    }

 

    public void accept() {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter title:- ");

        title = sc.nextLine();

        System.out.print("Please enter year:- ");

        year = sc.nextInt();

        System.out.print("Please enter rating between 0.0 to 5.0:- ");

        rating = sc.nextFloat();

    }

 

    public void display() {

        System.out.println("Movie Title:- " + title);

        if (rating >= 0 && rating <= 2.0) {

             System.out.println("Flop");

         } else if (rating >= 2.1 && rating <= 3.4) {

             System.out.println("Semi-hit");

         } else if (rating >= 3.5 && rating <= 4.5) {

             System.out.println("Hit");

         } else if (rating >= 4.6 && rating <= 5.0) {

            System.out.println("Super Hit");

        }

    }

 

    public static void main(String[] args) {

        movieMagic movie = new movieMagic();

        movie.accept();

        movie.display();

    }

 

}

 

         

7.      Define a class named movieMagic with the following description:

 

Instance variables/data members:

int year – to store the year of release of a movie

String title – to store the title of the movie.

float rating – to store the popularity rating of the movie.

(minimum rating = 0.0 and maximum rating = 5.0)

 

Member Methods:

 

(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to "".

(ii) void accept() To input and store year, title and rating.

(iii) void display() To display the title of a movie and a message based on the rating as per the table below.

 

RATING                                                                              MESSAGE TO BE DISPLAYED

 

0.0 to 2.0                                                                Flop

2.1 to 3.4                                                                Semi-hit

3.5 to 4.5                                                                Hit

4.6 to 5.0                                                                Super Hit

 

Write a main method to create an object of the class and call the above member methods.

 

SOLUTION

 

import java.util.Scanner;

class FruitJuice{

     int productCode;

    String flavour;

    String packType;

    int packSize;

    int productPrice;

    public void input()

    {

        Scanner sc= new Scanner(System.in);

        System.out.println("Please Enter Product code:- ");

        productCode = sc.nextInt();

        sc.nextLine(); // Consuming the left over line after integer in Scanner

        System.out.println("Please Enter Flavour:- ");

        flavour = sc.nextLine();

        System.out.println("Please Enter Pack type:- ");

        packType = sc.nextLine();

        System.out.print("Please Enter Pack size:- ");

        packSize = sc.nextInt();

        System.out.print("Please Enter Product price:- ");

        productPrice = sc.nextInt();

    }

    public void discount(){

        productPrice = productPrice-10;

    }

    public void display(){

        System.out.println("Product code:- " + productCode);

        System.out.println("Flavour:- " + flavour);

        System.out.println("Pack type:- " + packType);

        System.out.println("Pack size:- " + packSize);

        System.out.println("Product price:- " + productPrice);

    }

    public static void main(String args[]){

        FruitJuice obj = new FruitJuice();

        obj.input();

        obj.discount();

        obj.display();

    }

}

 

         

8.      Define a class called Library with the following description:

 

Instance variables/data members:

 

Int acc_num – stores the accession number of the book

String title – stores the title of the book stores the name of the author

 

Member Methods:

 

(i) void input() – To input and store the accession number, title and author.

(ii)void compute – To accept the number of days late, calculate and display and fine charged at the rate

of Rs.2 per day.

(iii) void display() To display the details in the following format:

Accession Number Title Author

 

Write a main method to create an object of the class and call the above member methods

 

SOLUTION

 

import java.util.Scanner;

 

class Library {

 

    int acc_num;

    String title;

    String author;

 

    public void input()  {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter accession number: ");

        acc_num = sc.nextInt();

        sc.nextLine();// Consuming the left over line after integer in Scanner

        System.out.print("Please enter title: ");

        title = sc.nextLine();

        System.out.print("Please enter author: ");

        author = sc.nextLine();

 

    }

 

    public void compute()  {

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter number of days late: ");

        int lateDays = sc.nextInt();

        System.out.println("Fine is Rs " + 2 * lateDays);

    }

 

    public void display() {

        System.out.println("Accession Number\tTitle\tAuthor");

        System.out.println("\t"+acc_num + "\t\t" + title + "\t" + author);

    }

 

    public static void main(String[] args)  {

        Library ob = new Library();

        ob.input();

        ob.compute();

        ob.display();

    }

}

 

 

9.      Define a class Mobike with the following description:

Instance variables/data members:

 

int bno: to store the bike’s number.

int phno: to store the phone number of the customer.

String name: to store the name of the customer.

int days: to store the number of days the bike is taken on rent.

int charge: to calculate and store the rental charge.

 

Member functions/methods:

 

void input(): to input and store the detail of the customer.

void compute(): to compute the rental charge.

The rent for a Mobike is charged on the following basis:

 

First five days:         Rs. 500 per day.

Next five days:         Rs. 400 per day.

Rest of the days:      Rs. 200 per day.

 

void display(): to display the details in the following format:

Bike No    Phone No    Name    No. of days    Charge

———–       ————–       ———     —————–         ———–

 

SOLUTION

 

import java.util.Scanner;

class Mobike{

     int bno;

     int phno;

     String name;

     int days;

     int charge;

    public void input(){

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter Bike number: ");

        bno = sc.nextInt();

        System.out.print("Please enter Phone number: ");

        phno = sc.nextInt();

        sc.nextLine();//Consuming the left over line after integer in Scanner

        System.out.print("Please enter Customer name: ");

        name = sc.nextLine();

        System.out.print("Please enter Number of days: ");

        days = sc.nextInt();

    }

    public void compute(){

        if(days <= 5)

            charge = days * 500;

        else if(days <= 10)

            charge = 2500 + (days - 5) * 400;

        else if(days > 10)

            charge = 4500 + (days - 10) * 300;

    }

    public void display(){

        System.out.println("Bike No.\tPhone No.\tName\tNo. of days\tCharge");

        System.out.println(bno + "\t\t" + phno + "\t\t" + name + "\t\t"+days+"\t" + charge);

    }

    public static void main(String args[]){

        Mobike ob = new Mobike();

        ob.input();

        ob.compute();

        ob.display();

    }

}

 

 

10.  Define a class Student as given below:

 

Data members/instance variables:

 

name: to store the student’s name.

age: to store the age.

m1, m2, m3: to store the marks in three subjects.

maximum: to store the highest marks among three subjects.

average: to store the average marks.

 

Member functions:

 

(i) A parameterized constructor to initialize the data members.

(ii) To accept the details of a student.

(iii) To compute the average and the maximum out of three marks.

(iv) To display all the details of the student.

 

Write a main() method to create an object of the class and call the above methods accordingly to enable the task.

 

SOLUTION

 

import java.util.Scanner;

class Student{

    String name;

    int age;

    int m1;

    int m2;

    int m3;

    int maximum;

    double average;

    public Student(String n, int a, int x, int y, int z){

        name = n;

        age = a;

        m1 = x;

        m2 = y;

        m3 = z;

        maximum = m1;

        average = 0.0;

    }

    public void input(){

        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter Name: ");

        name = sc.nextLine();

        System.out.println("Please enter Age: ");

        age = sc.nextInt();

        System.out.println("Please enter Marks 1: ");

        m1 = sc.nextInt();

        System.out.println("Please enter Marks 2: ");

        m2 = sc.nextInt();

        System.out.println("Please enter Marks 3: ");

        m3 = sc.nextInt();

    }

    public void compute(){

        average = (m1 + m2 + m3) / 3.0;

        if(m1>m2 && m1>m3)

            maximum = m1;

        else if(m2>m1 && m2>m3)

            maximum=m2;

        else

            maximum=m3;

    }

    public void display(){

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

        System.out.println("Marks 1: " + m1);

        System.out.println("Marks 2: " + m2);

        System.out.println("Marks 3: " + m3);

        System.out.println("Average: " + average);

        System.out.println("Maximum: " + maximum);

    }

    public static void main(String args[]){

        Student ob = new Student("", 0, 0, 0, 0);

        ob.input();

        ob.compute();

        ob.display();

    }

}

 

 

11.  An electronics shop has announced the following seasonal discounts on the purchase of certain items:

 

Purchase amount in Rs.            Discount on Laptop         Discount on Desktop PC

 

0 – 25000                                                0.0%                                     5.0%

25001 – 57000                                        5.0%                                     7.6%

57001 – 100000                                      7.5%                                     10%

More than 100000                  10.0%                                   15.0%

 

 

Write a program based on the above criteria to input name, address, amount of purchase and the type of purchase

(L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a customer along

with his name and address.

 

(Hint: Discount = (discount rate / 100) * amount of purchase, Net amount = amount of purchase – discount)

 

SOLUTION

import java.util.Scanner;

class Shop{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

        System.out.print("Name: ");

        String name = sc.nextLine();

        System.out.print("Address: ");

        String address = sc.nextLine();

        System.out.print("Amount of purchase: ");

        double amount = sc.nextDouble();

        System.out.println("Please enter type of purchase:");

        System.out.print("L for Laptop and D for Desktop");

 

        char type = sc.next().charAt(0);

        type=Character.toUpperCase(type);

 

        double discount = 0.0;

        if(amount <= 25000){

            if(type == 'L')

                discount = 0.0;

            else if(type == 'D')

                discount = 5.0;

        }

        else if(amount <= 57000){

            if(type == 'L')

                discount = 5.0;

            else if(type == 'D')

                discount = 7.6;

        }

        else if(amount <= 100000){

            if(type == 'L')

                discount = 7.5;

            else if(type == 'D')

                discount = 10.0;

        }

        else{

            if(type == 'L')

                discount = 10.0;

            else if(type == 'D')

                discount = 15.0;

        }

        discount = discount / 100 * amount;

        double net = amount - discount;

        System.out.println("Customer's name: " + name);

        System.out.println("Address: " + address);

        System.out.println("Net Amount: " + net);

    }

}

 

No comments:

Post a Comment