🌙 DARK

RECTANGLE DIMENSION CHANGE - INSTANCEOF OPERATOR - oops java

 

RECTANGLE DIMENSION CHANGE - INSTANCEOF OPERATOR

Rectangle Dimension Change
 
Write a Java program to illustrate the method of returning objects by getting details from the user and check the type of objects using instanceof and display these details in a detailed view using the following classes and methods.

[Note :
Strictly adhere to the object-oriented specifications given as a part of the problem statement.
Follow the naming conventions as mentioned. Create separate classes in separate files.]


Consider a class Rectangle with the following private member variables/attributes.

 
Data TypeVariable
Integerlength
Integerwidth

Include appropriate getters and setters.
Prototype for the Parameterized Constructor,

public Rectangle(Integer length, Integer width)

The Rectangle class includes the following methods.


Method Name
Integer area( )
void display( )
Rectangle dimensionChange(Integer newDimension)

Consider the class Main and write a main() method to test the above class.

In the main( ) method,

 
  • Display the area of the rectangle inside the main() method.
  • Obtain the details of the user.
  • Create an object for the Rectangle class using the parameterized constructor(length, width).

Problem Constraints:

Use instanceof operator to check the object returned by dimensionChange( ) method.
[The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).]


The link to download the template code is given below
Code Template

Input and Output Format:

Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output.]


Sample Input and Output :

Enter the length of the rectangle
5
Enter the width of the rectangle
6
Rectangle Dimension
Length:5
Width:6
Area of the Rectangle:30
Enter the new dimension
2
Rectangle Dimension
Length:10
Width:12
Area of the Rectangle:120



Problem Requirements:

Java

KeywordMin CountMax Count
instanceof1-







SOLUTION: 

CREATE NEW CLASS RECTANGLE:


public class Rectangle {
   
private int length;
    private int width;
    public Rectangle() {
        super();
        // TODO Auto-generated constructor stub
    }
    

    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public Rectangle(int length, int width) {
        super();
        this.length = length;
        this.width = width;
    }

public Integer area() {

int area = length*width;
        
        return area;

}
public void display(){
System.out.println("Rectangle Dimension");
        System.out.println("Length:"+getLength()+"\nWidth:"+getWidth());

}
Rectangle dimensionChange(Integer newDimension){

Rectangle rectangleObject = new Rectangle();
        int len = getLength();
        rectangleObject.setLength(newDimension*len);
        int wid = getWidth();
        rectangleObject.setWidth(newDimension*wid);
        return rectangleObject;
}

}




CREATE NEW CLASS MAIN: 


import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)  {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the length of the rectangle");
            int length = sc.nextInt();
            System.out.println("Enter the width of the rectangle");
             int width = sc.nextInt();
             Rectangle obj= new Rectangle(length,width);
             obj.display();
             int ar = obj.area();
             System.out.println("Area of the Rectangle:"+ar);
             System.out.println("Enter the new dimension");
             int d = sc.nextInt();
             Rectangle obj1 = obj.dimensionChange(d);
             obj1.display();
            System.out.println("Area of the Rectangle:"+obj1.area());
            boolean flag = obj1 instanceof Rectangle;
            
}
}





Display Item Type


The International Film Festival of India (IFFI), founded in 1952, is one of the most significant film festivals in Asia. The festival is for a week and arrangements have to be made for food, chairs, tables, etc. The organizing committee plans to deposit the advance amount to the contractors on confirmation of booking.

Write a Java program to get item type, cost per day, and deposit amount from the user and display these details in a detailed view using the following classes and methods.
 
[Note :Strictly adhere to the object-oriented specifications given as a part of the problem statement.
Follow the naming conventions as mentioned. Create separate classes in separate files.]


Consider a class named ItemType.
It must have the following private member 
variables/attributes.
 
Data TypeVariable
Stringname
DoublecostPerDay
Doubledeposit

Include the appropriate getters and setters.

The ItemType class includes the following method.

 
Method name
public void display()

Description
This method should display Item type details’
 followed by 
the details of the ItemType in the
 format as shown in the sample output.



Consider the class Main. It includes the method main
Write a code in the main method to test the ItemType class.
The following must be done inside the main method to test the ItemType class.
  • Get the item type details as input.
  • Create an ItemType Object with the given details using the setters of ItemType and call the display( ) method.
  • The itemType details need to be displayed in the display() method

 

Please use the below sample convention to create getters and setters of the class ItemType

 private String name; 
public String getName( ) {
         return name;
 }
 public void setName(String name) { 
        this.name = name; 
} 

 

Input and Output Format:
Refer sample input and output for formatting specifications.


Note:
Cost per day and Deposit value should be displayed up to 2 decimal places.


[All text in bold corresponds to input and the rest corresponds to output.]

Sample Input and Output 1:
Enter the item type name
Catering
Enter the cost per day
25000.00
Enter the deposit
10000.50
Item type details
Name 
: Catering
CostPerDay : 25000.00
Deposit : 10000.50

 



CREATE NEW CLASS ItemType:


import java.text.*;
import java.text.DecimalFormat;

public class ItemType {
    
    private String name;
private Double costPerDay;
private Double deposit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getCostPerDay() {
return costPerDay;
}
public void setCostPerDay(Double costPerDay) {
this.costPerDay = costPerDay;
}
public Double getDeposit() {
return deposit;
}
public void setDeposit(Double deposit) {
this.deposit = deposit;
}
public void display(){

DecimalFormat df = new DecimalFormat("0.00");
   
System.out.println("Item type details");
System.out.println("Name : "+name);
System.out.println("CostPerDay : "+df.format(costPerDay));
System.out.println("Deposit : "+df.format(deposit));
}
}




CREATE NEW CLASS MAIN:

import java.io.*;
import java.util.Scanner;

class Main{
    public static void main(String[] args) throws Exception{ 
        //ItemType obj = new ItemType();

Scanner sc = new Scanner(System.in);
        ItemType obj = new ItemType();    
System.out.println("Enter the item type name");
String name = sc.nextLine();
obj.setName(name);
        
//obj.display();
System.out.println("Enter the cost per day");
Double costPerDay = sc.nextDouble();
obj.setCostPerDay(costPerDay);
//obj.display();
System.out.println("Enter the deposit");
Double deposit = sc.nextDouble();
obj.setDeposit(deposit);
obj.display();
}
}




Customer Class With Constructor

 

Constructors are member functions that are called when an object is created.

 

Write a program to get the customer details, assign the values to the object using constructors and display it.

[Note: Strictly adhere to the object-oriented specifications given as a part of the problem statement.
Follow the naming conventions as mentioned. Create separate classes in separate files.]

Consider a class named Customer with the following public member variables
 

Data TypeVariable Name
StringcustomerName
StringcustomerEmail
StringcustomerType
StringcustomerAddress

Include default and parameterized  for the above class
Prototype for the parameterized constructor,
Customer(String customerName, String customerEmail, String customerType, String customerAddress)


Include the following method in the Customer class
 

Method NameDescription
void displayDetails()To display the details of the customer in the given format.


Consider the Main class to include the main() method.

In the Main method

  • Obtain the details of the customer.
  • Create an object for Customer class using parameterized constructor(customerName, customerEmail, customerType, customerAddress)
  • Call the method displayDetails() in the Main class.


The link to download the template code is given below
Code Template


Input and Output Format:


Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to the output]
 

Sample Input and Output 1:

Enter the Customer Details
Enter the name
Rose
Enter the email
rose@mail.com
Enter the type
Domestic
Enter the location
India

Name: Rose
E-mail: rose@mail.com
Type: Domestic
Location: India


Sample Input and Output 2:

Enter the Customer Details
Enter the name
Kate
Enter the email

kate@a.com
Enter the type

Domestic
Enter the location

Australia
Name: Kate
E-mail: kate@a.com
Type: Domestic
Location: Australia


CREATE NEW CLASS Customer:


public class Customer{
String customerName;
String customerEmail;
String customerType;
String customerAddress;



public Customer(String customerName, String customerEmail, String customerType, String customerAddress) {
this.customerName = customerName;
this.customerEmail = customerEmail;
this.customerType = customerType;
this.customerAddress = customerAddress;
}
public void displayDetails(){
System.out.println("Name: "+customerName);
System.out.println("E-mail: "+customerEmail);
System.out.println("Type: "+customerType);
System.out.println("Location: "+customerAddress);

}
}


CREATE NEW CLASS MAIN:

import java.io.*;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws Exception, IOException {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the Customer Details");
System.out.println("Enter the name");
String customerName = sc.nextLine();
System.out.println("Enter the email");
String customerEmail = sc.nextLine();
System.out.println("Enter the type");
String customerType = sc.nextLine();
System.out.println("Enter the location");
String customerAddress = sc.nextLine();
Customer obj = new Customer(customerName, customerEmail, customerType, customerAddress);
     obj.displayDetails();
}
}


Post a Comment

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form