uk20
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class Continent
{
String con;
InputStreamReader i = new InputStreamReader(System.in);
BufferedReader r = new BufferedReader(i);
void con_input() throws IOException
{
System.out.println("Enter Continent Name: ");
con = r.readLine();
}
}
class Country extends Continent
{
String cou ;
void cou_input() throws IOException
{
System.out.println("Enter Country Name: ");
cou = r.readLine();
}
}
class State extends Country
{
String sta;
void sta_input() throws IOException
{
System.out.println("Enter State Name: ");
sta = r.readLine();
}
}
class Main extends State
{
String pla;
void pla_input()throws IOException
{
System.out.println("Enter Place Name : ");
pla = r.readLine();
}
public static void main( String argsp[] )throws IOException
{
Main s = new Main();
s.con_input();
s.cou_input();
s.sta_input();
s.pla_input();
System.out.println("\n\nContinent: "+s.con);
System.out.println("Country: "+s.cou);
System.out.println("State: "+s.sta);
System.out.println("Place :" + s.pla);
}
}
Q2) Write a package for Operation, which has two classes, Addition and
Maximum. Addition has two methods add () and subtract (), which are used to
add two integers and subtract two, float values respectively. Maximum has a
method max () to display the maximum of two integers
SOLUTION:
package Operation;
import java.util.Scanner; //import Scanner class for user input
public class Addition {
//method to add two integers
public static int add(int num1, int num2) {
return num1 + num2;
}
//method to subtract two float values
public static float subtract(float num1, float num2) {
return num1 - num2;
}
}
public class Maximum {
//method to find the maximum of two integers
public static int max(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
}
public class TestOperation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two integers to add: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int sum = Addition.add(num1, num2);
System.out.println("The sum is: " + sum);
System.out.println("Enter two float values to subtract: ");
float num3 = scanner.nextFloat();
float num4 = scanner.nextFloat();
float difference = Addition.subtract(num3, num4);
System.out.println("The difference is: " + difference);
System.out.println("Enter two integers to find the maximum: ");
int num5 = scanner.nextInt();
int num6 = scanner.nextInt();
int maximum = Maximum.max(num5, num6);
System.out.println("The maximum is: " + maximum);
scanner.close(); //close the scanner to prevent resource leak
}
}