0% found this document useful (0 votes)
5 views11 pages

Java Programming Assignment Solutions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Java Programming Assignment Solutions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA ASSIGNMENT

[Link] a program that accept string as command line argument and generate the output in
specific way Example if two command line argument are wipro and bangolre then the ouput
generated should be Wipro technology Banglore. If the argument are ABC and Mumbai then
output should be ABC technology Mumbai.

SOL: package ASSIGN3;

import [Link];

public class QUESTION1 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

if ([Link] < 2) {

[Link]("Please provide two command line arguments.");

} else {

String output = args[0] + " technology " + args[1];

[Link]("Output: " + output);

[Link]();

OUTPUT: Output: Wipro technology Bangalore

[Link] a function that takes three argument from user and return their greater number out
of them to the calling program.

SOL: package ASSIGN3;

import [Link];
public class QUESTION2 {

public static int findGreatest(int a, int b, int c) {

return (a > b && a > c) ? a : (b > c ? b : c);

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int x = [Link]();

[Link]("Enter second number: ");

int y = [Link]();

[Link]("Enter third number: ");

int z = [Link]();

int result = findGreatest(x, y, z);

[Link]("Greatest number is: " + result);

[Link]();

OUTPUT: Enter first number: 25

Enter second number: 52

Enter third number: 45

Greatest number is: 52


[Link] if string contains valid number example.
SOL: package ASSIGN3;

import [Link];

public class QUESTION3 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");

String input = [Link]();

try {

[Link](input);

[Link]("Valid number.");

} catch (NumberFormatException e) {

[Link]("Invalid number.");

[Link]();

OUTPUT: Enter a string: 1111

Valid number.

[Link] Java String Object to Boolean Object.

SOL: package ASSIGN3;

import [Link];
public class QUESTION4 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter true or false: ");

String input = [Link]();

Boolean bool = [Link](input);

[Link]("Converted Boolean: " + bool);

[Link]();

OUTPUT: Enter true or false: TRUE

Converted Boolean: true

[Link] maximum of two numbers using [Link]

SOL: package ASSIGN3;

import [Link];

public class QUESTION5 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();
int max = [Link](a, b);

[Link]("Maximum is: " + max);

[Link]();

OUTPUT: Enter first number: 10

Enter second number: 11

Maximum is: 11

[Link] a class student1 which has fields name, age, marks . Initialize the variable using
parameterized constructor. Print all the data of a student inside show method of class record
student using message passing. Call show method is main.

SOL: package ASSIGN3;

import [Link];

class student1 {

String name;

int age;

double marks;

student1(String name, int age, double marks) {

[Link] = name;

[Link] = age;

[Link] = marks;

}
class recordstudent {

void show(student1 s) {

[Link]("Name: " + [Link]);

[Link]("Age: " + [Link]);

[Link]("Marks: " + [Link]);

public class QUESTION6 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter name: ");

String name = [Link]();

[Link]("Enter age: ");

int age = [Link]();

[Link]("Enter marks: ");

double marks = [Link]();

student1 s = new student1(name, age, marks);

recordstudent rs = new recordstudent();

[Link](s);

[Link]();

OUTPUT: Enter name: VIRAT

Enter age: 35

Enter marks: 75
Name: VIRAT

Age: 35

Marks: 75.0

[Link] singleton class.

SOL: package ASSIGN3;

class Singleton {

private static Singleton instance;

private Singleton() {

[Link]("Singleton class created.");

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

return instance;

public void showMessage() {

[Link]("Hello from Singleton class!");

public class QUESTION7 {

public static void main(String[] args) {

Singleton s1 = [Link]();

Singleton s2 = [Link]();

[Link]("Are both classes same? " + (s1 == s2));


[Link]();

OUTPUT: Singleton class created.

Are both classes same? true

Hello from Singleton class!

8. Write a program to display all the prime numbers from 1 to 20.

SOL: package ASSIGN3;

import [Link];

public class QUESTION8 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Prime numbers between 1 and 20:");

for (int i = 2; i <= 20; i++) {

boolean prime = true;

for (int j = 2; j <= i / 2; j++) {

if (i % j == 0) {

prime = false;

break;

if (prime)

[Link](i + " ");

}
[Link]();

OUTPUT: Prime numbers between 1 and 20:

2 3 5 7 11 13 17 19

9. Kiwi Inc. is a leading software development company in the US. The top management of
the organization has found that the Help Desk division does not handle queries or issues of
the employees on a priority basis. For this, the top management has decided to automate
the tasks of the Help Desk division. Therefore, the management has assigned this task to the
development team to create an application that has the following functionalities:
Application must enable employees to log their requests or issues. Application must enable
the team of the Help Desk division to handle queries on the first come first served basis. The
development team has assigned the task to John, the Senior Software Developer. However,
in the initial phase of development, John needs to create the user interface and create the
application only for a single user. Help John to achieve the preceding requirement.

SOL: package ASSIGN3;

import [Link];

class HelpDesk {

String[] requests = new String[5];

int front = 0, rear = 0;

public void logRequest(String request) {

if (rear < [Link]) {

requests[rear] = request;

rear++;

[Link]("Request logged successfully.");

} else {

[Link]("Request queue is full. Please process existing requests.");

}
}

public void processRequest() {

if (front < rear) {

[Link]("Processing request: " + requests[front]);

front++;

} else {

[Link]("No requests to process.");

public class QUESTION10 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

HelpDesk helpDesk = new HelpDesk();

[Link]("Welcome to Kiwi Inc.");

while (true) {

[Link]("\nChoose an option:");

[Link]("1. Log a new request");

[Link]("2. Process next request");

[Link]("3. Exit");

[Link]("Your choice: ");

int choice = [Link]();

[Link]();

switch (choice) {

case 1:

[Link]("Enter your request: ");

String req = [Link]();


[Link](req);

break;

case 2:

[Link]();

break;

case 3:

[Link]("Exiting Help Desk. Goodbye!");

return;

default:

[Link]("Invalid choice. Try again.");

OUTPUT: Welcome to Kiwi Inc.

Choose an option:

1. Log a new request

2. Process next request

3. Exit

Your choice: 1

Enter your request: ASSIGNMENT

Request logged successfully.

Choose an option:

1. Log a new request

2. Process next request

3. Exit

Your choice:

Common questions

Powered by AI

To optimize the readability or performance of the method for determining the greatest number among three inputs, using the Math.max function twice can simplify the code. This reduces nested ternary operations into `int greatest = Math.max(a, Math.max(b, c));`, enhancing clarity and enabling potential compiler optimizations for performance .

The automation of the Help Desk division tasks demonstrates principles such as efficiency, scalability, and modularity. Efficiency is achieved as it allows queries to be handled systematically on a first-come-first-served basis, reducing manual effort. Scalability is shown in the potential to expand the system beyond a single user. Modularity is evident in separating logging and processing functionality within the HelpDesk class, promoting manageability and extensibility .

Parameterized constructors in Java allow for initializing object data when instances of a class are created, ensuring that the object has a valid initial state. In the 'student1' class, the parameterized constructor takes 'name', 'age', and 'marks' as arguments, initializing the object's state directly upon creation, which helps in managing and accessing the data easily in later operations .

`Boolean.valueOf` is preferable in some cases because it returns a Boolean object rather than a primitive type, which is necessary when dealing with object collections or when nullability is required. Unlike `Boolean.parseBoolean` which returns a primitive boolean, `Boolean.valueOf` provides the flexibility of storing a reference type for further object-oriented manipulations .

The program uses a try-catch block to attempt parsing the string into a Double. If parsing fails, a NumberFormatException is thrown, indicating an invalid number. This method is limited to recognizing only numbers that are valid Double values; it doesn't handle numbers outside the Double range or locale-specific formatting (e.g., commas as thousands separators).

The Singleton design pattern demonstrates the concept of restricting the instantiation of a class to one object. It is useful because it ensures that only one instance of the class exists throughout the application, which is particularly advantageous when a single object needs to coordinate actions across the system, like logging or managing a shared resource .

The 'HelpDesk' class simulates a queue system using an array, where 'front' and 'rear' pointers indicate the start and end of the queue, respectively. Requests are processed in a first-come-first-served manner. To improve it, use a dynamic data structure like a LinkedList to handle an unlimited number of requests. Implementing circular queue logic could also optimize array usage by wrapping around the indices, thereby using available spaces efficiently .

Using command line arguments allows for easy input passing without requiring user interaction during runtime, which is useful for automation and scripting. In the string manipulation example, these arguments enable dynamic output generation based on user input. However, the downsides include restricted input size, lack of user-friendly input validation, and the potential for errors if arguments are not passed in the expected format or order .

In the 'student1' class, combining parameterized constructors with method overloading enhances flexibility, allowing different ways to instantiate objects. For instance, overloading constructors to accept varying sets of parameters (e.g., only name and age, or all fields) can enable creation of objects even when some data is missing or default values are applicable. This approach facilitates better management of object lifecycle and default initialization .

Static methods for instance management, as seen in the Singleton pattern, limit flexibility because they inherently prevent inheritance (subclass creation) and increase coupling by hard-coding access mechanisms. This can affect testability and extendability. Additionally, they do not fully leverage multi-threaded environments unless synchronized properly, which can lead to performance bottlenecks or require more complex code for safe initialization .

You might also like