Objective:
- Introduction to Java
- How to compile Java code
- Simple arithmetic problems solving
Activity 1:
Take two inputs from user (keyboard) length and breadth of rectangle. Compute the area of rectangle given by: area = length*breadth
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int length = 0;
int breadth = 0;
int area = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Length and Breadth of Rectangle");
length = scanner.nextInt();
breadth = scanner.nextInt();
area = length * breadth;
System.out.println("The Area is = " + area);
scanner.close();
}
}
Activity 2:
Write Java code to solve the second-degree equation aX^2 + bX + c= 0 for any real numbers a, b and c. The program takes the values of a, b and c as input from the user, and calculates X.
Note: real numbers are taken in float.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
float a = 0;
float b = 0;
float c = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three Real Numbers a, b and c: ");
a = scanner.nextFloat();
b = scanner.nextFloat();
c = scanner.nextFloat();
float eq1;
float sq;
float eq2;
float eq3;
eq1 = (b * b) - (4 * a * c);
sq = (float) Math.sqrt(eq1);
eq2 = (-b - sq) / (2 * a);
System.out.println("The First value of X is = " + eq2);
eq3 = (-b + sq) / (2 * a);
System.out.println("The Second value of X is = " + eq3);
scanner.close();
}
}