Professional Documents
Culture Documents
LABS
LABS
LABS
1- (Display message) Write a java program that prints the text "Hello
world".
*
**
***
J A V V A
J AA V V AA
J J AAAAA V V AAAAA
JJ A A V A A
6- (Print a table) Write a Java program that displays the following table:
a a^2 a^3
1 1 1
2 4 8
3 9 27
4 16 64
1. Write a Java program that takes two numbers as input and display the
product of two numbers.
Test Data:
Input first number: 25
Input second number: 5
Expected Output:
25 x 5 = 125
Test Data:
input radius: 7.5
Expected Output:
Perimeter is = 47.12388980384689
Area is = 176.71458676442586
Test Data:
input width: 5.6
input height: 8.5
Expected Output:
Area is 5.6 * 8.5 = 47.599999999999994
Perimeter is 2 * (5.6 + 8.5) = 28.20
4. Write a Java program to convert temperature from Fahrenheit to Celsius
degree.
celsius = (( 5 *(fahrenheit - 32.0)) / 9.0)
Test Data:
Input a degree in Fahrenheit: 212
Expected Output:
212.0 degree Fahrenheit is equal to 100.0 in Celsius
5. Write a Java program that reads an integer between 0 and 1000 and adds
all the digits in the integer.
Test Data:
Input an integer between 0 and 1000: 565
Expected Output :
The sum of all digits in 565 is 16
6. Write a Java program to convert minutes into a number of years and days.
Test Data:
Input the number of minutes: 3456789
Expected Output :
3456789 minutes is approximately 6 years and 210 days
7. Write a Java program that reads a number and display the square, cube,
and fourth power.
Test Data:
Input the number: 2
Expected Output :
Square: 4
Cube: 8
Fourth power: 16
Assignment #1:
Given three assignment marks, each of which is marked out of 20.
Write a Java program to calculate and print the average mark out of 100.
Test Data:
Input Mark1: 15
Input Mark1: 20
Input Mark1: 10
Expected Output :
The average mark out of 100 = 75.0
Lab 3
Math and String Methods
Ex1:
Write a program that generates two random integer numbers between -20 and 20 and displays
the maximum number after finds the absolutes values for negative numbers.
Solution:
int x=-20 + (int)(Math.random() * 41);
int y=-20 + (int)(Math.random() * 41);
System.out.println(x+"\n"+y);
x=Math.abs(x);
y=Math.abs(y);
int z=Math.max(x, y);
System.out.println("The max value is:"+z);
Ex2:
Write a program that receives an ASCII code (an integer between 0 and 127) and displays its
character.
Solution:
Scanner input = new Scanner(System.in);
System.out.print("Enter an ASCII code: ");
int code = input.nextInt();
System.out.println("The character for ASCII code " + code + " is " + (char)code);
Assignment:
Write a Java program to prompt the user to enter a line and output the following:
• How many characters in the line?
• The fifth character (if exists)
• All characters in uppercase
• All characters in lowercase
Solution:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
s = s.trim();
int length = s.length();
System.out.println("Length is: " + length);
if (length >= 5)
System.out.println("Fifth character is: " + s.charAt(4));
else System.out.println("The fifth character does not exist");
System.out.println("Uppercase: " + s.toUpperCase()); System.out.println("Lowercase: " +
s.toLowerCase());
Lab 3
1. Write a Java program to accept two integers and check whether they are
equal or not.
7. Write an if-statement that takes two integer variables and exchanges their
values if the first one is greater than the second one.
Assignment #2: