JAVA Assignment
JAVA Assignment
Set
1)Write a program to calculate perimeter and area of rectangle.
(hint : area = length * breadth , perimeter=2*(length+breadth)
Ans=>
import java.util.Scanner;
public class Rectangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Length of Rectangle : ");
int length = sc.nextInt();
System.out.println("Enter breadth of Rectangle : ");
int breadth = sc.nextInt();
int area = length * breadth;
System.out.println("Area of Reactangle : " + area);
int Perimeter = 2 * (length + breadth);
System.out.println("Perimeter of Reactangle : " + Perimeter);
sc.close();
}
}
Output:-
SET B
1)Write a java program to display the system date and time in various
formats shown below:
Ans=>
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatter {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("MM-dd-yyyy");
Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("EEEE MMMM dd yyyy");
Str = sdf.format(date);
System.out.println("Current date is: " + Str);
sdf = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
Str = sdf.format(date);
System.out.println("Current date and time is: " + Str);
sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");
Str = sdf.format(date);
System.out.println("Current date and time is: " + Str);
sdf = new SimpleDateFormat("hh:mm:ss");
Str = sdf.format(date);
System.out.println("Current time is: " + Str);
sdf = new SimpleDateFormat("w");
Str = sdf.format(date);
System.out.println("Current week of year is: " + Str);
sdf = new SimpleDateFormat("W");
Str = sdf.format(date);
System.out.println("Current week of the month is: " + Str);
sdf = new SimpleDateFormat("D");
Str = sdf.format(date);
System.out.println("Current day of the year: " + Str);
}
}
Output:-
Output:-(2)
Output:-(3)
Set C
1) Write a program to accept n names of country and display them in
descending
order.
Ans=>
import java.util.*;
public class countrysort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter total no of Countries: ");
int n = sc.nextInt();
String country[] = new String[n];
sc.nextLine();
for (int i = 0; i < n; i++) {
System.out.println("Enter country no :" + (i + 1));
country[i] = sc.nextLine();
}
System.out.println("Country names without Sorting:");
for (String ele : country) {
System.out.println("" + ele);
}
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (country[i].compareTo(country[j]) < 0) {
temp = country[i];
country[i] = country[j];
country[j] = temp;
}}}
System.out.println("Countries in Descending order:");
for (String ele : country) {
System.out.println("" + ele);
}
sc.close();
}}
Output:-
case 2:
sum = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (j>i) {
sum = sum+m[i][j];
}
}
}
System.out.println("\nAddition of Upper Diagonal Elements:"+sum);
break;
case 3:
sum = 0;
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m.length; j++) {
if (i>j) {
sum = sum+m[i][j];
}
}
}
System.out.println("\nAddition of Lower Diagonal Elements:"+sum);
break;
case 4:
System.exit(0);
default:
break;
}
} while (true);
}
}
Output:-
3) Write a program to display the 1 to 15 tables.
(1*1=1 2 * 1 = 2 ........ 15 * 1 = 15
1*2=2 2*2=4 15 * 2 = 30
1*3=3 2*3=6 15 * 3 = 45
1 * 10 = 10 2 * 10 = 20 15 * 10 = 150)
Ans=>
public class table {
public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
System.out.println("Table of "+(i)+" number:");
for (int j = 1; j <= 10; j++) {
System.out.println(i + "*" + j + "=" +i*j);
}
System.out.println("----------------------");
}}}
Output:-