You are on page 1of 5

Index

Contents
Assignment: LA 1.1.............................................................................................................................2
Question 1.........................................................................................................................................2
1. Problem statement:..................................................................................................................2
2. Class Diagram:.........................................................................................................................2
3. Logic:........................................................................................................................................2
4. Inputs, Outputs and Error handling:.....................................................................................2
5. Test cases and results:.............................................................................................................2
6. Conclusion................................................................................................................................2
Question 2.........................................................................................................................................2
1. Problem statement:..................................................................................................................3
2. Class Diagram:.........................................................................................................................3
3. Logic:........................................................................................................................................3
4. Inputs, Outputs and Error handling:.....................................................................................3
5. Test cases and results:.............................................................................................................3
6. Conclusion................................................................................................................................3
Question 3.........................................................................................................................................4
1. Problem statement:..................................................................................................................4
2. Class Diagram:.........................................................................................................................4
3. Logic:........................................................................................................................................4
4. Inputs, Outputs and Error handling:.....................................................................................4
5. Test cases and results:.............................................................................................................4
6. Conclusion................................................................................................................................5

Page 1 UD/OOP/LR/v1.0
Assignment: LA 1.1

Course: CS593 – Object Oriented Programming


Department: Computer Science and Engineering, Techno Main Salt Lake
Name: Shreyashi Muhury Roll 13000119057
:

Question 1

1. Problem statement:
Write a Java Program to display “Hello World! I am learning Java.”

2. Class Diagram:
No separate classes were used in this program, we only used the main class.

3. Logic: 
To display text to the screen we used System.out.println().

4. Inputs, Outputs and Error handling: 


Inputs for the program Text (string)
Expected Outputs from the program Text (string)
Error handling (for preventing wrong Not required
data processing)

5. Test cases and results:


SL Test Case Expected Observed Status
Result Result
1 Enter value as “Hello world! Output should Output is OK
I am learning Java.” be “Hello “Hello world! I
world! I am am learning
learning Java.” Java.”

6. Conclusion 
Java System.out.println() is used to print an argument that is passed to it. The statement can
be broken into 3 parts which can be understood separately as:

 System: It is a final class defined in the java.lang package.

Page 2 UD/OOP/LR/v1.0
 out: This is an instance of PrintStream type, which is a public and static member field
of the System class.
 println(): As all instances of PrintStream class have a public method println(), hence
we can invoke the same on out as well. This is an upgraded version of print(). It prints
any argument passed to it and adds a new line to the output. We can assume that
System.out represents the Standard Output Stream.

Question 2

1. Problem statement:
Write a Java Program to input user’s name and greet the user as stated in the example.
Input Output
Mr. James Gosling Hello Mr. James Gosling

2. Class Diagram:
Scanner

+nextLine (): String


+close ()

3. Logic: 
i. We take input from user using Scanner class using scanner object sc and using
method nextLine() [ for String input ]
ii. Store that String inside a String variable called name
iii. Display on screen using System.out.println() and we used the format (“Hello”
+name+ “!”) where name is our variable.

4. Inputs, Outputs and Error handling: 


Inputs for the program Text (string)
Expected Outputs from the program Text (string)
Error handling (for preventing wrong Not required
data processing)

5. Test cases and results:


SL Test Case Expected Result Observed Result Status
1 Enter value as “Mr. James Output should be Output is “Hello Mr. OK
Gosling” “Hello Mr. James James Gosling!”
Gosling!”
2. Enter value as “Kabir Output should be Output is “Hello Kabir OK
Singh” “Hello Kabir Singh!”
Singh!”

6. Conclusion 
Scanner is a class in java.util package used for obtaining the input of the primitive types like
int, double, etc. and strings. It is the easiest way to read input in a Java program, though not
very efficient if you want an input method for scenarios where time is a constraint like in
competitive programming.

Page 3 UD/OOP/LR/v1.0
 To create an object of Scanner class, we usually pass the predefined object System.in,
which represents the standard input stream. We may pass an object of class File if we
want to read input from a file.
 To read numerical values of a certain data type XYZ, the function to use is
nextXYZ(). For example, to read a value of type short, we can use nextShort()
 To read strings, we use nextLine().
 To read a single character, we use next().charAt(0). next() function returns the next
token/word in the input as a string and charAt(0) function returns the first character in
that string.

Question 3

1. Problem statement:
Write a Java Program to two marks and display their average after ignoring the fractional part
(if any).
Input Output
50 31 40

2. Class Diagram:
Scanner

+ nextLine(): String
+close()

3. Logic: 
i. We take input from user using Scanner class using scanning object sc and using
method nextInt() [ for integer input ]
ii. We repeat step (i) for two times to take input of two numbers and save them in int
type variables named “a” and “b” respectively.
iii. We find the average by (a+b)/2 and save to int type variable “avg”
iv. We display the avg value using System.out.println()

4. Inputs, Outputs and Error handling: 


Inputs for the program a (int), b (int)
Expected Outputs from the program avg (int)
Error handling (for preventing wrong Display error when a < 0 or b < 0
data processing)

5. Test cases and results:


SL Test Case Expected Result Observed Result Status
1 Enter the values: Output of avg Output of avg is: OK
a: 50 should be:
b: 31 40 40

Page 4 UD/OOP/LR/v1.0
2. Enter the values: Output of avg Output of avg is: OK
a: 10 should be:
b: 20 15 15
3. Enter the values: Output of avg Output of avg is: OK
a: 21 should be:
b: 12 16 16
4. Enter the values: Output of avg Output of avg is: OK
a: 55 should be:
b: 45 50 50

6. Conclusion 
All the variables, a, b and avg, are declared. The a, b and avg variables are of int type and can
hold integer values. To take the input value from the end-user we have used the Scanner class
which is defined in the java.util.Scanner package but one can also take any input-output
stream class like BufferedReader class to read the input. To find the value of avg the formula
is used: a+b/avg. Using System.out.println(), the avg value is printed.

Page 5 UD/OOP/LR/v1.0

You might also like