December 22, 2024

Object Oriented Program and Design 2024 Q&A

Object Oriented Analysis and Design
Share :

Object Oriented Analysis and Design Question and Answers


( Suggestion :  keep refreshing the page for updated content & Search Questions Using Find )


Q.1. Imagine you are developing a program for a cutting-edge streaming service, and your goal is to can access specific content. This streaming service offers a 121wb86939-88485-2024/03/03 create variety of content, including movies and TV shows, and users subscribe at levels: either as Premium subscribers or Basic subscribers. The access conditions are provided below. different a system that determines whether a user • Premium Subscribers: Enjoy unrestricted access to all content, regardless of age Subscribers: Enjoy unrestricted access to all content, regardless of age Basic Subscribers. Access is determined by age restrictions • Movies. Available to users aged 18 and above. TV Shows Accessible to users aged 16 and above. Write a Java program that takes input from users – the type of content they want to access (input should match “Movies” or “TV Shows”), their age and their subscription level (input should match “basic” or “premium”). Nse the Scanner class for reading the input. The program should then determine whether the user has the right to access the requested content based on the conditions mentioned above. The program should handle potential invalid inputs, such as negative ages or subscription levels that are not recognized by the system.

ANSWER:-

import java.util.Scanner;

public class StreamingServiceAccess {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Prompt user for content type
System.out.println(“Enter the type of content you want to access (Movies/TV Shows):”);
String contentType = scanner.nextLine().trim();

// Prompt user for age
System.out.println(“Enter your age:”);
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline character

// Prompt user for subscription level
System.out.println(“Enter your subscription level (Basic/Premium):”);
String subscriptionLevel = scanner.nextLine().trim();

// Validate inputs
if (!isValidContentType(contentType) || age < 0 || !isValidSubscriptionLevel(subscriptionLevel)) {
System.out.println(“Invalid input. Please enter valid inputs.”);
return;
}

// Determine access
boolean hasAccess = false;
if (subscriptionLevel.equalsIgnoreCase(“Premium”)) {
hasAccess = true;
} else if (subscriptionLevel.equalsIgnoreCase(“Basic”)) {
if (contentType.equalsIgnoreCase(“Movies”)) {
hasAccess = age >= 18;
} else if (contentType.equalsIgnoreCase(“TV Shows”)) {
hasAccess = age >= 16;
}
}

// Output access result
if (hasAccess) {
System.out.println(“You have access to the requested content.”);
} else {
System.out.println(“You do not have access to the requested content.”);
}
}

// Method to validate content type
private static boolean isValidContentType(String contentType) {
return contentType.equalsIgnoreCase(“Movies”) || contentType.equalsIgnoreCase(“TV Shows”);
}

// Method to validate subscription level
private static boolean isValidSubscriptionLevel(String subscriptionLevel) {
return subscriptionLevel.equalsIgnoreCase(“Basic”) || subscriptionLevel.equalsIgnoreCase(“Premium”);
}
}





Q.2.Design a simple program in Java to create an exam schedule using interfaces. Define an interface named Exam with methods scheduleExam() and printSchedule(). Implement the interface in two classes: MathExam and History Exam. The scheduleExam() method should set the date and time for the respective exams, and printSchedule() should display the scheduled information. Finally create object for both math and history exams and demonstrate the scheduling and printing functionality

ANSWER:-

interface Exam {
void scheduleExam(String date, String time);
void printSchedule();
}

class MathExam implements Exam {
private String date;
private String time;

@Override
public void scheduleExam(String date, String time) {
this.date = date;
this.time = time;
}

@Override
public void printSchedule() {
System.out.println(“Math Exam Schedule:”);
System.out.println(“Date: ” + date);
System.out.println(“Time: ” + time);
}
}

class HistoryExam implements Exam {
private String date;
private String time;

@Override
public void scheduleExam(String date, String time) {
this.date = date;
this.time = time;
}

@Override
public void printSchedule() {
System.out.println(“History Exam Schedule:”);
System.out.println(“Date: ” + date);
System.out.println(“Time: ” + time);
}
}

public class Main {
public static void main(String[] args) {
MathExam mathExam = new MathExam();
mathExam.scheduleExam(“2024-04-10”, “10:00 AM”);
mathExam.printSchedule();

System.out.println();

HistoryExam historyExam = new HistoryExam();
historyExam.scheduleExam(“2024-04-12”, “11:00 AM”);
historyExam.printSchedule();
}
}





Q.3. A renowned Pizza Chain has automated its pizza making process. It wanted to processing of tasks: There are three tasks identified which can happen in parallel ( Vegetable Cutting, Dough Preparation and Preheating of oven)
A) Create a thread implementation with parallel threads for three above tasks running in parallel. For each task a separate class needs to be implemented and in run methods only task name should be printed to signify the task. Each thread should sleep for 1000 ms after task is completed.

B) Once these tasks are complete, pizza delivery task should be implemented similar to above but not as a separate thread.

ANSWER:- A.

class VegetableCutting implements Runnable {
@Override
public void run() {
System.out.println(“Vegetable Cutting”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class DoughPreparation implements Runnable {
@Override
public void run() {
System.out.println(“Dough Preparation”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class PreheatingOven implements Runnable {
@Override
public void run() {
System.out.println(“Preheating Oven”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class PizzaMaking {
public static void main(String[] args) {
Thread vegCuttingThread = new Thread(new VegetableCutting());
Thread doughPrepThread = new Thread(new DoughPreparation());
Thread preheatOvenThread = new Thread(new PreheatingOven());

vegCuttingThread.start();
doughPrepThread.start();
preheatOvenThread.start();

try {
vegCuttingThread.join();
doughPrepThread.join();
preheatOvenThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(“All tasks completed. Starting pizza delivery.”);
// Implement pizza delivery task here without using a separate thread
System.out.println(“Pizza delivery completed.”);
}
}

B.

  • We have three classes (VegetableCutting, DoughPreparation, and PreheatingOven) each implementing the Runnable interface for parallel execution of tasks.
  • In the run() method of each class, we print the name of the task and then sleep for 1000 milliseconds to simulate task completion.
  • In the PizzaMaking class, we create threads for each task and start them.
  • We use join() to wait for all three tasks to complete before proceeding with pizza delivery.
  • After all tasks are completed, we print a message indicating the start of pizza delivery.




Q.4. 1.import port java util Scanner, 2. 3. public class ReadDataDemo { 4. public static void main(String[] args) 5. { 6. Scanner 7 S new Scanner(System.in); 7. 8. String StuName = s. ; 9. char gender = s. ; 10. int age = s. ; 11. long Phone No = s. ; 12. double average = s. ; 13. 14. System.out.println(“Student Name: ” + StuName); 15. System.out.println(“Gender:” + gender); 16. System.out.println(“Age:” + age); 17 System.out.println(“Phone Number:”+ Phone No); 18. System.out.println(“Average: ” + average); 19. } 20. }

ANSWER:-

import java.util.Scanner;

public class ReadDataDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print(“Enter student name: “);
String stuName = s.nextLine();

System.out.print(“Enter gender: “);
char gender = s.next().charAt(0); // Assuming user inputs a single character for gender

System.out.print(“Enter age: “);
int age = s.nextInt();

System.out.print(“Enter phone number: “);
long phoneNumber = s.nextLong();

System.out.print(“Enter average: “);
double average = s.nextDouble();

System.out.println(“Student Name: ” + stuName);
System.out.println(“Gender: ” + gender);
System.out.println(“Age: ” + age);
System.out.println(“Phone Number: ” + phoneNumber);
System.out.println(“Average: ” + average);

// Close the scanner
s.close();
}
}





Q.5. a. Find Good Numbers using Java A number is termed as a “GOOD NUMBER” if meets the following two criflerna mentioned 1. All the digits in the number must be Conly 1 or 2 at 88485-2 iin. No two adjacent substrings of the number is same or Example for good numbers: 1, 121, 1213, 1213121 Example for bad numbers: 11, 1212, 1221, 1231231 11 is a bad number because 1 follows 1 • Similarly, 1212 is bad because the substring “12” is present adjacent to the substring “12” 1221 is a bad number because the substring “2” follows “2” 1231231 is a bad number because the substring “123” is adjacent to substring “123”
b. In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods. Observe and analyse.

ANSWER:-  A.

import java.util.ArrayList;
import java.util.List;

public class GoodNumbers {

public static void main(String[] args) {
int n = 10; // Change this to the desired number of good numbers to find
List<Integer> goodNumbers = findGoodNumbers(n);
System.out.println(“Good Numbers:”);
for (int num : goodNumbers) {
System.out.println(num);
}
}

public static List<Integer> findGoodNumbers(int n) {
List<Integer> goodNumbers = new ArrayList<>();
int num = 1;
while (goodNumbers.size() < n) {
if (isGoodNumber(num)) {
goodNumbers.add(num);
}
num++;
}
return goodNumbers;
}

public static boolean isGoodNumber(int num) {
String str = String.valueOf(num);
for (int i = 0; i < str.length() – 1; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
return false;
}
}
return true;
}
}

This program finds and prints the first n good numbers. You can change the value of n in the main method to find more or fewer good numbers.

B.

Regarding access modifiers in Java, there are four types: public, private, protected, and default (no modifier). These modifiers control the visibility of classes, interfaces, variables, methods, constructors, and setter methods:

  1. public: The entity is accessible from any other class.
  2. private: The entity is accessible only within the same class.
  3. protected: The entity is accessible within the same package and by subclasses (even if they are in different packages).
  4. Default (no modifier): The entity is accessible only within the same package.

These access modifiers help in encapsulation and defining the level of access to various components of a Java program.

 


For More Updates Join Our Channels :