Topic 2
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.]
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.]
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.
Consider the Main class and write the main method to test the above 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
Main.java:
import java.util.Scanner;
import java.io.*;
public class Main extends User {
[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 Main and write a main() method to test the above class.
Rectangle.java:
Main.java:
import java.io.*;
import java.util.*;
}
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.
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
int gcd = 1;
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.
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
}
}
Sample Output 1:
Arguments :
Command
Arguments
The number of arguments is 2
Sample Output 2:
Arguments :
Commands
The number of arguments is 1