0% found this document useful (0 votes)
7 views

21BEC0651 Exp4 Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

21BEC0651 Exp4 Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Meghana Kulkarni

Reg no. 21BEC0651


Question 1:
Code:
public class Car
{
void vehicleType()
{
System.out.println("Vehicle type: 4 wheeler");
}
}
public class Maruti extends Car
{
void Brand()
{
System.out.println("Brand: Maruti");
}
void speed()
{
System.out.println("Speed: 120kmph");
}
}
public class Baleno extends Maruti
{
void speed()
{
super.speed();
System.out.println("Speed: 100kmph");
}
public static void main(String args[])
{
Baleno obj=new Baleno();
obj.vehicleType();
obj.Brand();
obj.speed();
}
}
Output:
Question 2:
Code:
public class Calculations
{
static int sum;
static int diff;
public void add(int a, int b)
{
sum=a+b;
}
public void difference(int a, int b)
{
diff=a-b;
}
}
import java.util.Scanner;
public class My_Calcuations extends Calculations
{
public void multiplication(int a,int b)
{
int product=a*b;
System.out.println("Product= "+product);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.print("Enter 1st number: ");
int a=in.nextInt();
System.out.print("Enter 2nd number: ");
int b=in.nextInt();
My_Calcuations obj=new My_Calcuations();
obj.add(a, b);
System.out.println("Sum= "+sum);
obj.difference(a, b);
System.out.println("Difference= "+diff);
}
}
Question 3:
Code:
import java.io.*;
public class Person
{
String name;
String aadhar;
String phone;
int age;
void setter()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("ENTER DETAILS: ");
System.out.print("Name: ");
name=in.readLine();
System.out.print("Aadhar number: ");
aadhar=in.readLine();
System.out.print("Phone number: ");
phone=in.readLine();
System.out.print("Age: ");
age=Integer.parseInt(in.readLine());
}
static void sameName(String x[], String y[])
{
boolean found=false;
for(int i=0;i<x.length;i++)
{
for(int j=i+1;j<x.length;j++)
{
if(x[i].equalsIgnoreCase(x[j]))
{
found=true;
System.out.println("Found! "+x[i]+" is the common name for
registration numbers "+y[i]+" and "+y[j]);
}
}
}
if(found==false)
System.out.println("No common name found");
}
}
import java.io.*;
public class Student extends Person
{
String registrationNumber;
String school;
String prog;
double cgpa;
String dept;
public void setter()throws IOException
{
super.setter();
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.print("Registration number: ");
registrationNumber=in.readLine();
System.out.print("School name: ");
school=in.readLine();
System.out.print("Program: ");
prog=in.readLine();
System.out.print("Department: ");
dept=in.readLine();
System.out.print("CGPA: ");
cgpa=Double.parseDouble(in.readLine());
}
static void search(String regNumber,Student[]studArray)
{
boolean found=false;
for(int i=0;i<studArray.length;i++)
{
if(regNumber.equalsIgnoreCase(studArray[i].registrationNumber))
{
found=true;
System.out.println("FOUND!");
System.out.println("Name: "+studArray[i].name);
System.out.println("Aadhar: "+studArray[i].aadhar);
System.out.println("Phone number: "+studArray[i].phone);
System.out.println("Age: "+studArray[i].age);
System.out.println("School: "+studArray[i].school);
System.out.println("Program: "+studArray[i].prog);
System.out.println("CGPA: "+studArray[i].cgpa);
System.out.println("Department: "+studArray[i].dept);
break;
}
}
if(found==false)
System.out.println("NOT FOUND!");
}
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.print("Enter number of students: ");
int n=Integer.parseInt(in.readLine());
String names[]=new String[n];
String r[]=new String[n];
Student obj[]=new Student[n];
for(int i=0;i<n;i++)
{
obj[i]=new Student();
obj[i].setter();
names[i]=obj[i].name;
r[i]=obj[i].registrationNumber;
}
System.out.print("Enter registration number to be searched: ");
String find=in.readLine();
search(find,obj);
sameName(names,r);
}
}
Output:

You might also like