0% found this document useful (0 votes)
150 views16 pages

Topic 2

The program defines a Rectangle class with length and width attributes and methods to calculate area, display dimensions, and change dimensions. The Main class gets rectangle dimensions from the user, creates a Rectangle object, displays the initial area, changes one dimension, and uses instanceof to check the returned object type.

Uploaded by

Sai Hrudhay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
150 views16 pages

Topic 2

The program defines a Rectangle class with length and width attributes and methods to calculate area, display dimensions, and change dimensions. The Main class gets rectangle dimensions from the user, creates a Rectangle object, displays the initial area, changes one dimension, and uses instanceof to check the returned object type.

Uploaded by

Sai Hrudhay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

Topic-2

Q1.
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 Type Variable
String name
Double costPerDay
Double deposit

Include the appropriate getters and setters.

The ItemType class includes the following method.


Method name Description
public void display() This method should display ‘Item type details’ followed by the details of the ItemType

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

CODE:

ItemType.java

import java.text.DecimalFormat;
class ItemType {
private String name;
private Double costPerDay;
private Double deposit;
DecimalFormat d = new DecimalFormat("0.00");
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() {
System.out.println("Item type details");
System.out.println("Name : " + getName());
String s1 = d.format(getCostPerDay());
System.out.println("CostPerDay : " + s1);
String s = d.format(getDeposit());
System.out.println("Deposit : " + s);
}

}
Main.java:

import java.util.*;
import java.text.DecimalFormat;
public class Main extends ItemType {
public static void main(String[] args) {
// DecimalFormat df = new DecimalFormat(".##");
ItemType r = new ItemType();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the item type name");
r.setName(sc.nextLine());
System.out.println("Enter the cost per day");
r.setCostPerDay(sc.nextDouble());
System.out.println("Enter the deposit");
r.setDeposit(sc.nextDouble());
r.display();

}
Q2.
Compare Phone Number
New App helps you discover great places to eat around or de-stress in all major cities
across 20000+ merchants. Explore restaurants, spa & salons, and activities to find your
next fantastic deal. Write a program to find the duplication of user accounts.

Write a Java program to get two users’ details and display whether their phone numbers
are the same or not with the following class 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 the class User with the following private attributes/variables.

Date Type Variable


String name
String username
String password
Long phoneNumber

Include appropriate getters and setters.


Prototype for the Parameterized Constructor,
public User(String name, String username, String password, Long phoneNumber)

Define the following method in the User class.

Method Name Description


public boolean comparePhoneNumber(User user) In this method, compare the phone number of the t

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

In the main method

 Obtain the details of the user.


 Create an object for the User class using the parameterized constructor(name,
username, password, phoneNumber).
 Call the method comparePhoneNumber() 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.


If both phone numbers are the same then print “Same Users” else print “Different
Users”.
The output should be printed in the Main class.

Sample Input/Output 1:
[All text in bold corresponds to the input and the rest corresponds to output.]

Enter Name
john
Enter UserName
john@123
Enter Password
john@123
Enter PhoneNumber
9092314562
Enter Name
john
Enter UserName
john@12
Enter Password
john@12
Enter PhoneNumber
9092314562
Same Users

Sample Input/Output 2:

Enter Name
william
Enter UserName
william####
Enter Password
william
Enter PhoneNumber
9092314562
Enter Name
john
Enter UserName
john@123
Enter Password
john@123
Enter PhoneNumber
9092312102
Different Users
CODE:

User.java

public class User {


private String name;
private String userName;
private String password;
private long phoneNumber;
public User (String name, String userName, String password,long phoneNumber) {
this.name=name;
this.userName=userName;
this.password=password;
this.phoneNumber=phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return userName;
}
public void setUsername(String username) {
this.userName = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhonenumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
} public boolean comparePhoneNumber(User user){
if(getPhoneNumber()==user.phoneNumber){
return true;
}else {
return false;
}
}
}

Main.java:

import java.util.Scanner;
import java.io.*;
public class Main extends User {

public Main(String name1, String username1, String password1, Long phonenumbe


r1) {
super(name1, username1, password1, phonenumber1);

public static void main(String[] args) {


Scanner a=new Scanner(System.in);
System.out.println("Enter Name");
String z=a.nextLine();
System.out.println("Enter UserName");
String y=a.nextLine();
System.out.println("Enter Password");
String x=a.nextLine();
System.out.println("Enter PhoneNumber");
long w=a.nextLong();
System.out.println("Enter Name");
String b=a.nextLine();
String c=a.nextLine();
System.out.println("Enter UserName");
String d=a.nextLine();
System.out.println("Enter Password");
String e=a.nextLine();
System.out.println("Enter PhoneNumber");
long f=a.nextLong();
if(f==w) {
System.out.println("Same Users");
}else {
System.out.println("Different Users");}
}
}
Q3.
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 Type Variable
Integer length
Integer width

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 Description
Integer area( ) This method computes the area of the rectangle and
void display( ) This method displays the length and width of the re
Rectangle dimensionChange(Integer newDimension) This method changes the rectangle dimension by in

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
CODE:

Rectangle.java:

public class Rectangle {


private Integer length;
private Integer width;
public Rectangle (Integer length, Integer width) {
this.length= length;
this.width= width;
}
public Integer area() {
return this.length*this.width;
}
public void display() {
System.out.println("Rectangle Dimension");
System.out.println("Length:" + this.length);
System.out.println("Width:" + this.width);
}Rectangle dimensionChange(Integer newDimension) {
Rectangle rectangleObject = new Rectangle(this.length * newDimension, this.w
idth * newDimension);
return rectangleObject;
}
}

Main.java:

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

public class Main {


public static void main(String[] args) throws Exception, IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the rectangle");
Integer len = sc.nextInt();
System.out.println("Enter the width of the rectangle");
Integer wid= sc.nextInt();
Rectangle rect = new Rectangle(len, wid);
rect.display();
System.out.println("Area of the Rectangle:" + rect.area());
System.out.println("Enter the new dimension");
Integer n= sc.nextInt();
Rectangle r = rect.dimensionChange(n);
if (r instanceof Rectangle) {
r.display();
System.out.println("Area of the Rectangle:" + r.area());
}

}
Q4.
Simplified Fraction
St. Patrick Convent organizes a project exhibition "Innovative Minds" every year with an
objective to provide the platform and unleash the potential of the students by
showcasing their innovative projects. Pasha is a smart high school student and was
eager to participate in the fair for the first time.

After a lot of ground works, she decided her project and set out to design the same. Her
project requirement was to design an advanced calculator that has a fraction feature
that will simplify fractions. The project will accept a non-negative integer as a numerator
and a positive integer as a denominator and outputs the fraction in simplest form. That
is, the fraction cannot be reduced any further, and the numerator will be less than the
denominator.

Help Pasha to program her advanced calculator and succeed in her first ever project
presentation. You can assume that all input numerators and denominators will produce
valid fractions.

Hence create a class named Fraction with the following method.

Method Name Description


void printValue(int,int) This method should display the fraction in simplest form.

Create a driver class called Main. In the Main method, obtain input from the user in the
console and call the printValue method present in the Fraction class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem
statement.
All class names, attribute names and method names should be the same as specified in
the problem statement. Create separate classes in separate files.]

Input Format:
First line of the input is a non-negative integer which is the numerator in the fraction.
Second line of the input is a positive integer which is thedenominator in the fraction.

Output Format:
Output the simplified form of the fraction in a single line.
Refer sample input and output for formatting specifications.

Sample Input 1:
28
7
Sample Output 1:
4

Sample Input 2:
13
5

Sample Output 2:
2 3/5
CODE:

Main.java

import java.util.*;
public class Main{
public static void main(String args[]){
//get inputs
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int den=sc.nextInt();
Fraction.printValue (num,den);
}

Fraction.java

public class Fraction{


public static void printValue (int num,int den)
{
if (num<=0){
System.out.println(0);
}
else if (num<den) {
int gcd=getGCD(num,den);
System.out.println(num/gcd+"/"+den/gcd);
}
if (num==den) {
System.out.println(num/den);
}
else if (num>den) {
int rem=num%den;
int quo=(int) (num/den);
if(rem!=0) {
int gcd=getGCD(rem,den);
System.out.println(quo+" "+rem/gcd+"/"+den/gcd);
}
else {
System.out.println(quo);
}
}
}
private static int getGCD (int num, int den){

int gcd = 1;

for (int i= Math.min (num, den); i>=2; i--) {


if((num%i==0)&&(den %i==0)) {
gcd=i;
break;
}
}
return gcd;
}

Q5.
Array

Write a Java program to display the array of Integers and array of Strings. Use for each
loop to iterate and print the elements.
Constraints :
Use for each loop to iterate and print the elements.
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 n :
3
Enter numbers :
100
23
15
Enter strings :
hi
hello
welcome
Displaying numbers
100
23
15
Displaying strings
hi
hello
welcome
CODE:

Main.java:

import java.io.*;
import java.util.*;
public class Main{
public static void main (String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter n :");
int size = sc.nextInt();
String[] ar = new String[size];
int[] arr = new int[size];
System.out.println("Enter numbers : ");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Enter strings : ");
for (int i = 0; i < size; i++) {
ar[i] = sc.next();
}
System.out.println("Displaying numbers");
for (int i : arr) {
System.out.println(i);
}
System.out.println("Displaying strings");
for (String i : ar) {
System.out.println(i);
}
//Fill your code
}
}

Command Line Argument - Count


Write a program to accept strings as command-line arguments and print the number of
arguments entered.

Sample Input (Command Line Argument) 1:


Command Arguments

Sample Output 1:
Arguments :
Command
Arguments
The number of arguments is 2

Sample Input (Command Line Argument) 2:


Commands

Sample Output 2:
Arguments :
Commands
The number of arguments is 1

public class Main{


public static void main(String[] args){
int count=0;
System.out.println("Arguments :");
for(int a=0;a<args.length;a++)
{
System.out.println(args[a]);
count++;
}
System.out.println("The number of arguments is "+count);
}
}

You might also like