Obj Oriented Prog & Design Question and Answers
( Suggestion : keep refreshing the page for updated content & Search Questions Using Find )
Q1.A.Createclass called Wase, implement a code that creates unique id for each wane student using default constructorB) Fill in the blanks to complete the following code segment to generate the following output
ANSWER:- A)
import java.util.UUID;
public class Wase {
private String studentId;
// Default constructor
public Wase() {
// Generate a unique ID using UUID
this.studentId = UUID.randomUUID().toString();
}
// Getter method for studentId
public String getStudentId() {
return studentId;
}
// Example usage
public static void main(String[] args) {
Wase student1 = new Wase();
Wase student2 = new Wase();
System.out.println(“Student 1 ID: ” + student1.getStudentId());
System.out.println(“Student 2 ID: ” + student2.getStudentId());
}
}
B)
public class Bits {
public static void main(String[] args) {
Pilani p = new Pilani(“SomeLocation”);
}
}
class Pilani {
Pilani() {
System.out.println(“Default constructor called”);
}
Pilani(String location) {
this(); // Call the default constructor
System.out.println(“Constructor with location called: ” + location);
}
}
public static void main(String a[])
{
Employee E = new Employee();
E.name = “John”;
E.id = 1;
E.sal = 50000.0;
E.display();
}
ANSWER:-import java.util.Scanner;
public class TaxCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter Income of person: “);
double income = scanner.nextDouble();
double tax = calculateTax(income);
double surcharge = calculateSurcharge(tax);
System.out.println(“\nIncome = ” + income);
System.out.println(“TAX = ” + tax);
System.out.println(“Surcharge = ” + surcharge);
scanner.close();
}
private static double calculateTax(double income) {
double tax = 0;
if (income <= 50000) {
tax = 0;
} else if (income <= 100000) {
tax = (income – 50000) * 0.10;
} else if (income <= 250000) {
tax = 50000 * 0.10 + (income – 100000) * 0.20;
} else {
tax = 50000 * 0.10 + 150000 * 0.20 + (income – 250000) * 0.30;
}
return tax;
}
private static double calculateSurcharge(double tax) {
double surcharge = 0;
if (tax > 10000) {
surcharge = tax * 0.02;
}
return surcharge;
}
}
ANSWER:-import java.util.Scanner;
public class TaxCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter Income of person: “);
double income = scanner.nextDouble();
double tax = calculateTax(income);
double surcharge = (tax > 10000) ? 0.02 * tax : 0;
System.out.println(“\nIncome = ” + income);
System.out.println(“TAX = ” + tax);
System.out.println(“Surcharge = ” + surcharge);
scanner.close();
}
private static double calculateTax(double income) {
double tax = 0;
if (income <= 50000) {
tax = 0;
} else if (income <= 100000) {
tax = 0.1 * (income – 50000);
} else if (income <= 250000) {
tax = 0.1 * (100000 – 50000) + 0.2 * (income – 100000);
} else {
tax = 0.1 * (100000 – 50000) + 0.2 * (250000 – 100000) + 0.3 * (income – 250000);
}
return tax;
}
}
Q.3.Powerball is a lottery played in many of the United States. The lottery numbers are chosen randomly from two containers numbered balls. Five white balls are chosen from a container of 49 balls, and one red ball is chosen from a container of 42 balls Write a program that simulates the selection of the Powerball lottery numbers. Use the Math.random() function to simulate the random selection of a numbered ball. Keep in mind that you cannot just randomly generate six numbers. The five white numbers must be unique and between 1 and 45. (After a ball is removed from the container, it can’t be selected again.). The one red number must be between 1 and 42. Note that the red number can possibly be the same number as one of the selected white balls Make this Powerball game as object-oriented program by writing a class called Powerball. i. Write a class named Powerball that contains two data members that is a reference to an array of type integer to represent the five white halls and one more field to represent a red ball. ii. Add a method named play() that simulates the playing of Powerball iii. This method should assign a value to each of the six data members. iv. Add a method named displayResults() that prints out the values of the five white balls and one red ball v. Use a for loop and the length attribute of arrays when displaying the results in the display Results() method. vi. write a driver class named PlayLottery that contains main()method. Within Main() method, instantiate a Powerball object and involved methods of the Powerball class to ensure that they are wicking successfully
ANSWER:-
import java.util.Arrays;
class Powerball {
private int[] whiteBalls;
private int redBall;
public Powerball() {
whiteBalls = new int[5];
redBall = 0;
}
public void play() {
// Simulate selection of five unique white balls between 1 and 45
for (int i = 0; i < whiteBalls.length; i++) {
int randomWhiteBall;
do {
randomWhiteBall = (int) (Math.random() * 45) + 1;
} while (contains(whiteBalls, randomWhiteBall));
whiteBalls[i] = randomWhiteBall;
}
// Simulate selection of one red ball between 1 and 42
redBall = (int) (Math.random() * 42) + 1;
}
public void displayResults() {
System.out.println(“White Balls: ” + Arrays.toString(whiteBalls));
System.out.println(“Red Ball: ” + redBall);
}
private boolean contains(int[] array, int value) {
for (int i : array) {
if (i == value) {
return true;
}
}
return false;
}
}
public class PlayLottery {
public static void main(String[] args) {
Powerball powerball = new Powerball();
powerball.play();
powerball.displayResults();
}
}
Q.4.