You are on page 1of 11

CSE108 Object Oriented Programming Lab

Activity 2: Fundamentals of Java Programming


Topics covered: Java Programming – Java Basics, Selections, Booleans, If Statements
Software to be used: NetBeans IDE / Online …

Roll No.AU2140054 Name: Jineet Shah Section No.: 03

1. Write a Java program to get a number from the user and print whether it is positive
or negative.
Input
import java.util.Scanner;
public class Activity21 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num>0){
System.out.println("The number is positive.");
}
else if (num<0){
System.out.println("The number is negative.");
}
else{
System.out.println("The number is 0.");
}}

}
OUTPUT
Enter a number: 12
The number is positive.

2. Take three numbers from the user and print the greatest number.
INPUT
import java.util.Scanner;
public class Activity23 {
public static void main(String[] args) {
int num1;
int num2;
int num3;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number 1: ");
num1 = sc.nextInt();
System.out.print("Enter number 2: ");
num2 = sc.nextInt();
System.out.print("Enter number 3: ");
num3 = sc.nextInt();
if (num1>num2 && num1>num3){
System.out.println(num1+" is the greatest number.");
}
else if (num2>num1 && num2>num3){
System.out.println(num2+" is the greatest number.");
}
else{
System.out.println(num3+" is the greatest number.");
}
}
}
OUTPUT
Enter number 1: 12
Enter number 2: 32
Enter number 3: -34
32 is the greatest number.
3. Write a Java program that reads a number between 1 to 7 from the user and displays
the name of the weekday.

Test Data
Input number: 2
Expected Output:
Tuesday
INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
int a;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number from 1 to 7: ");
a=sc.nextInt();
if (a==1){
System.out.println("Monday");
}
else if (a==2){
System.out.println("Tuesday");
}
else if (a==3){
System.out.println("Wednesday");
}
else if (a==4){
System.out.println("Thursday");
}
else if (a==5){
System.out.println("Friday");
}
else if (a==6){
System.out.println("Saturday");
}
else if (a==7){
System.out.println("Sunday");
}
else{
System.out.println("Out of range input");
}
}
}
OUTPUT
Enter a number from 1 to 7: 6
Saturday
4. Write a Java program that takes the user to provide a single character from the
alphabet. Print Vowel or Consonant, depending on the user input.
INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
char ch;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a character: ");
ch=sc.next().charAt(0);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||
ch=='O'||ch=='U'){
System.out.println("Vowel");
}
else{
System.out.println("Consonant");
}
}
}
OUTPUT
Enter a character: I
Vowel
5. Write Java program to allow the user to input his/her age. Then the program will
show if the person is eligible to vote. A person who is eligible to vote must be older
than or equal to 18 years old.
INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
int a;
Scanner sc=new Scanner(System.in);
System.out.print("Enter your age: ");
a=sc.nextInt();
if (a>=18){
System.out.println("You are eligible to vote.");
}
else{
System.out.println("You are not eligible to vote.");
}
}
}
OUTPUT
Enter your age: 28
You are eligible to vote.
6. Write a Java program to determine whether an input number is an even number.
INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
int a;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
a=sc.nextInt();
if (a%2==0){
System.out.println("EVEN NUMBER");
}
else{
System.out.println("ODD NUMBER");
}
}
}
OUTPUT
Enter a number: 12349804
EVEN NUMBER
7. Write a Java program that determines a student’s grade. Read marks of a student.
Display corresponding grade.

-if marks >= 90% => grade = A


-if marks >= 70% and < 90% => grade = B
-if marks >= 40% and < 70% => grade = C
-if marks < 40% => grade = I
INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
float a;
Scanner sc=new Scanner(System.in);
System.out.print("Enter your maerks: ");
a=sc.nextFloat();
if (a>=90){
System.out.println("GRADE A");
}
else if (a>=70){
System.out.println("GRADE B");
}
else if (a>=40 ){
System.out.println("GRADE C");
}
else{
System.out.println("GRADE I");
}
}
}
OUTPUT
Enter your maerks: 89
GRADE B

8. Write a Java program to swap two variables.

INPUT
import java.util.Scanner;
public class Activity233 {
public static void main(String[] args) {
float a,b,c;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number 1: ");
a=sc.nextFloat();
System.out.print("Enter number 2: ");
b=sc.nextFloat();
c=b;
b=a;
a=c;
System.out.println("Number1= "+a);
System.out.println("Number1= "+b);
}
}
OUTPUT
Enter number 1: 11
Enter number 2: 34
Number1= 34.0
Number1= 11.0
9. Evaluate the following method calls [Use of Math class and it’s methods]:
 Math.sqrt(4)
 Math.sin(2 * Math.PI)
 Math.cos(2 * Math.PI)
 Math.pow(2, 2)
 Math.max(2, Math.min(3, 4))
 Math.ceil(-2.5)
 Math.floor(-2.5)
 Math.round(2.5)
 Math.round(Math.abs(-2.5))

public class MATHSS {

public static void main(String[] args) {

System.out.println(Math.sqrt(4));

System.out.println(Math.sin(2 * Math.PI));

System.out.println(Math.cos(2 * Math.PI));

System.out.println(Math.pow(2, 2));

System.out.println(Math.max(2, Math.min(3, 4)));


System.out.println(Math.ceil(-2.5));

System.out.println(Math.floor(-2.5));

System.out.println(Math.round(2.5));

System.out.println(Math.round(Math.abs(-2.5)));

OUTPUT

2.0

-2.4492935982947064E-16

1.0

4.0

-2.0

-3.0

10. Use of random() method


a. Write an expression to print a random integer between 30 and 40.
INPUT
import java.util.*;
public class MATHSS {
public static void main(String[] args) {
int max=40;
int min=30;
Random rand=new Random();
int randomNum=rand.nextInt((max-min))+min+1;
System.out.println(randomNum);
}}
OUTPUT
32
b. Write an expression to print a random integer between 0 and 999.
INPUT
import java.util.*;
public class MATHSS {
public static void main(String[] args) {
int max=999;
int min=0;
Random rand=new Random();
int randomNum=rand.nextInt((max-min))+min+1;
System.out.println(randomNum);
}}
OUTPUT
586

You might also like