uk5
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)
package Practicals;
import java.io.*;
public class Ass1_setB2 {
static BufferedReader sc = new BufferedReader(new
InputStreamReader(System.in));
// static int i,j;
// return mat_add;
static int a[][] = new int[5][5];
static int a2[][] = new int[5][5];
public static void main(String[] args) throws NumberFormatException,
IOException {
int m = 0, n = 0;
System.out.println("------Create two Matrix-------");
System.out.print("Enter number of rows ");
m = Integer.parseInt(sc.readLine());
System.out.print("Enter number of columns ");
n = Integer.parseInt(sc.readLine());
if (m == n) {
int i, j;
System.out.println(
"Enter values for 1st matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
a[i][j] = Integer.parseInt(sc.readLine());
}
}
System.out.println(
"Enter values for 2nd matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
a2[i][j] = Integer.parseInt(sc.readLine());
}
}
System.out.println("1st Matrix :");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
System.out.print("[" + a[i][j] + "]");
}
System.out.println("");
}
System.out.println("2nd Matrix :");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
System.out.print("[" + a2[i][j] + "]");
}
System.out.println("");
}
} else {
System.out.println("Rows not equal to columns");
}
int ch;
do {
System.out.println("\nChoose the matrix operation,");
System.out.println("----------------------------");
System.out.println("1. Addition");
System.out.println("2. Multiplication");
System.out.println("3. Transpose");
System.out.println("4. Exit");
System.out.println("----------------------------");
System.out.print("Enter your choice: ");
ch = Integer.parseInt(sc.readLine());
switch (ch) {
case 1:
int mat_add[][] = new int[m][n];
int i1, j1;
System.out.println("Addition of Matrix 1 and Matrix 2 is ");
for (i1 = 0; i1 < m; i1++) {
for (j1 = 0; j1 < n; j1++) {
mat_add[i1][j1] = a[i1][j1] + a2[i1][j1];
System.out.print("[" + mat_add[i1][j1] + "]");
}
System.out.println("");
}
break;
case 2:
int product[][] = new int[m][n];
System.out.println("Multiplication of Matrix 1 and Matrix 2 = ");
int i2, j2;
for (i2 = 0; i2 < m; i2++) {
for (j2 = 0; j2 < n; j2++) {
product[i2][j2] = 0;
for (int k = 0; k < m; k++) {
product[i2][j2] += a[i2][k] * a2[k][j2];
}
System.out.print("[" + product[i2][j2] + "]");
}
System.out.println("");
}
break;
case 3:
int transpose[][] = new int[m][n];
int transpose2[][] = new int[m][n];
System.out.println("Transpose of Matrix 1 and 2 = ");
int i3, j3;
for (i3 = 0; i3 < m; i3++) {
for (j3 = 0; j3 < n; j3++) {
transpose[i3][j3] = a[j3][i3];
transpose2[i3][j3] = a2[j3][i3];
System.out.print("[" + transpose[i3][j3] + "]");
System.out.print("[" + transpose2[i3][j3] + "]");
}
System.out.println("");
}
break;
case 4:
System.exit(0);
default:
System.out.println("Enter Valid choice !");
System.exit(0);
break;
}
} while (ch != 4);
}