LABS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab 1:

1- (Display message) Write a java program that prints the text "Hello
world".

2- (Display three messages) Write a Java program that displays


Welcome to Java, Welcome to Computer Science, and Programming
is fun.

3- (Display five messages) Write a Java program that displays Welcome


to Java five times.

4- (Display a pattern) Write a Java program that displays the following


pattern:

*
**
***

5- (Display a pattern) Write a Java program that displays the following


pattern:

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

7- (Summation of a series) Write a Java program that displays the result


of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.
Lab 2

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

2. Write a Java program to print the area and perimeter of a circle.


area = PI * radius * radius
perimeter = 2 * PI * radius

Test Data:
input radius: 7.5
Expected Output:
Perimeter is = 47.12388980384689
Area is = 176.71458676442586

3. Write a Java program to print the area and perimeter of a rectangle


area = width * height
perimeter = 2*(height + width)

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.

2. Write a Java program to check whether a given number is positive or


negative.

3. Write a Java program to find the largest of three numbers?

4. Write a Java program to read temperature and display a suitable message


according to temperature state below :
Temp >=40 then Its Very Hot
Temp >=30 then Its Hot
Temp >=20 then Normal in Temp
Temp >=10 then Cold weather
Temp >=0 then Very Cold weather
else Freezing weather

5. Write a Java program to check whether an alphabet is a vowel or consonant.


6. Write a program in Java which is a Menu-Driven Program to perform a
simple calculation.

Enter the first Integer :10


Enter the second Integer :2

Here are the options :


1-Addition.
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.

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:

Write a Java program to print the division of certificate. The division is


determined as follows:
Rate from 0 to less than 2:”Fail”
Rate from 2 to less than 3:”Third”
Rate from 3 to less than 4:”Second”
Rate from 4 to 5:”First”

You might also like