Experiment 1
Write a program to perform following operations on two numbers input by the
user:
1) Addition 2) subtraction 3) multiplication 4) division
import java.util.Scanner;
public class Calculate
{
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;
case 5:
System.exit(0);
}
}
}
}
Experiment 2 - Write a Java program to compute area of:
1) Circle2) rectangle 3) triangle 4) square
import java.util.Scanner;
class OverloadDemo
{
void area(int x)
{
System.out.println("the area of the square is "+x*x+" sq units");
}
void area(int x, int y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(float x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
void area(double x, double y)
{
double tarea = (x*y)/2;
System.out.println("the area of the triange is "+tarea+" sq units");
}
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
OverloadDemo ob = new OverloadDemo();
System.out.println("Enter 1 to find area of square \n");
System.out.println("Enter 2 to find area of rectangle \n");
System.out.println("Enter 3 to find area of circle \n");
System.out.println("Enter 4 to find area of triangle \n");
System.out.println("Enter 5 to Exit");
int opt = s.nextInt();
switch(opt)
{
case 1:
int side;
System.out.println("Enter the length of Side of Square");
side = s.nextInt();
ob.area(side);
break;
case 2:
int len,wid;
System.out.println("Enter length of rectangle");
len = s.nextInt();
System.out.println("Enter width of rectangle");
wid = s.nextInt();
ob.area(len,wid);
break;
case 3:
float Radius;
System.out.println("Enter radius of the circle");
Radius = s.nextFloat();
ob.area(Radius);
break;
case 4:
double base, height;
System.out.println("Entre base of triangle");
base = s.nextDouble();
System.out.println("Entre height of triangle");
height = s.nextDouble();
ob.area(base,height);
break;
case 5:
System.exit(0);
}
}
}
Experiment 3:- Write a program to convert temperature from Fahrenheit to
Celsius degree using Java.
import java.util.Scanner;
class fahrenheittocelsius {
public static void main(String[] args)
{
double fahrenheit=0.0, celsius = 0.0;
Scanner s = new Scanner(System.in);
System.out.println(
"Enter the value in fahrenheit");
fahrenheit = s.nextDouble();
celsius = ((fahrenheit - 32) *5)/9;
System.out.println(
"value of temperature in celsius:" + celsius);
}
}
Experiment 4:- Write a Java program to determine greatest number of three
numbers.
import java.util.Scanner;
public class LargestNumberExample1
{
public static void main(String[] args)
{
int a, b, c, largest, temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
temp=a>b?a:b;
largest=c>temp?c:temp;
System.out.println("The largest number is: "+largest);
}
}
Experiment 5:- Write program that gets a number from the user and generates
an integer between 1 and 7 subsequently should display the name of the
weekday as per that number.
import java.util.Scanner;
public class Experiment5 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int day = in.nextInt();
System.out.println(getDayName(day));
}
// Get the name for the Week
public static String getDayName(int day) {
String dayName = "";
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default:dayName = "Invalid day range";
}
return dayName;
}
}
Experiment 6:- Construct a Java program to find the number of days in a
month.
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] strings) {
Scanner s = new Scanner(System.in);
int DaysInMonth = 0;
String NameOfMonth = "Unknown";
System.out.print("Input a month number: ");
int month = s.nextInt();
System.out.print("Input a year: ");
int year = s.nextInt();
switch (month) {
case 1:
NameOfMonth = "January";
DaysInMonth = 31;
break;
case 2:
NameOfMonth = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
DaysInMonth = 29;
} else {
DaysInMonth = 28;
}
break;
case 3:
NameOfMonth = "March";
DaysInMonth = 31;
break;
case 4:
NameOfMonth = "April";
DaysInMonth = 30;
break;
case 5:
NameOfMonth = "May";
DaysInMonth = 31;
break;
case 6:
NameOfMonth = "June";
DaysInMonth = 30;
break;
case 7:
NameOfMonth = "July";
DaysInMonth = 31;
break;
case 8:
NameOfMonth = "August";
DaysInMonth = 31;
break;
case 9:
NameOfMonth = "September";
DaysInMonth = 30;
break;
case 10:
NameOfMonth = "October";
DaysInMonth = 31;
break;
case 11:
NameOfMonth = "November";
DaysInMonth = 30;
break;
case 12:
NameOfMonth = "December";
DaysInMonth = 31;
}
System.out.print(NameOfMonth + " " + year + " has " + DaysInMonth + "
days\n");
}
}
Experiment 7:- Construct a Java program to implement parametrized
constructor in java .
class exp7 {
String languages;
exp7(String lang) {
languages = lang;
System.out.println(languages + " Programming Language");
}
public static void main(String args[]) {
// call constructor by passing a single value
exp7 obj1 = new exp7("Java");
exp7 obj2 = new exp7("Python");
exp7 obj3 = new exp7("C");
}
}
Experiment 8:- Construct a Java program to implement copy constructor in
java .
import java.util.Scanner;
import java.io.*;
class A
{
int a;
String b;
A()
{
a=10;
b="Kritika";
System.out.println(a+""+b);
}
A(B ref)
{
a=ref.d;
b=ref.e;
System.out.println(a+""+b);
}
}
class B
{
int d;
String e;
B()
{
d=30;
e="Manraj";
System.out.println(d+""+e);
}
}
class C
{
public static void main(String args[])
{
A r = new A();
B r1 = new B();
A r2 = new A(r1);
}
}