3. Sample Input: Sample Output:
Name : RamKumar
Address : Kollam
Passport Number : J7546891
Question 1
Write a java program which contains class Passport and class Person.
Compose the class Passport in the class Person. Class Passport contains
default constructor which sets the name, address and passport no. Display
the name of the person, Address and passport number in class Person.
RamKumar
Kollam
J7546891
5. public class Person
{
Passport passport;
Person(Passport passport){
this .passport = passport;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
String Address = sc.nextLine();
String Number = sc.next();
Passport p = new Passport(name,Address,Number);
Person person = new Person(p);
System.out.println("Name : " + person.passport.Name);
System.out.println("Address : " + person.passport.Address);
System.out.println("Passport Number : " + person.passport.Number);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
6. Question 2
Write a java program which contains class Heart and class Person.
Compose the class Heart in the class Person. Class Heart contains default
constructor which sets the status of the heart is “Beating” or “Stopped” and
print the status of the heart. In class Person check whether the heart is
beating or stopped.
If it is beating print “Heart is Beating”.
else
Print “Heart beat is Stopped” and make the heart object null.
print the status of the class heart .
7. Sample Input: Sample Output:
Heart is Beating
Beating
Question 2
Beating
Sample Input: Sample Output:
Heart beat is Stopped
Human is Dead
Stopped
9. public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String Status = sc.nextLine();
Heart beat = new Heart(Status);
Human H = new Human(beat);
if(H.beat.Status.equals("Beating"))
{
System.out.println("Heart is Beating");
}
else {
H = null;
System.out.println("Heart beat is Stopped");
}
try{
System.out.println(H.beat.Status);
}
catch(NullPointerException x){
System.out.println("Human is Dead");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
10. Sample Input: Sample Output:
Laptop Name: DELL
Operating System : Windows 10
Question 3
Write a java program which contains class Operating_System, Laptop and
Main. Compose the class Operating_System in class Laptop. Class
Operating_System gets the name of the Operating System. Class Laptop
gets the name of the laptop. In class Main, object for class Laptop is created
and print’s the name of the laptop and name of Operating System.
DELL
Windows 10
12. class Laptop
{
private String Name;
private Operating_System Os;
public Laptop()
{
Os = new Operating_System();
}
public void setName(String Name)
{
this.Name = Name;
}
public Operating_System getOperating_System()
{
return Os;
}
public String toString()
{
return "Laptop Name: " + Name + "nOperating System : " + Os ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
13. public class Main {
public static void main(String args[]) {
Laptop laptop = new Laptop();
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
String os = sc.nextLine();
laptop.setName(name);
laptop.getOperating_System().setOs(os);
System.out.println(laptop);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
14. Question 4
Write a java program which contains the class Battery and class Mobile.
Compose the class Battery in class Mobile. In the Class Battery, get
percentage of the charge in the battery. In class Mobile, check the battery
percentage and display the following message.
If battery percentage == 0
Print “Phone is switched Off”
Else if battery percentage lies between 5 to 20.
Print “Please charge your phone”
Else
Print “No need of charging”
15. import java.util.Scanner;
class Battery
{
int percentage;
public Battery(int percentage)
{
this.percentage=percentage;
}
}
public class Mobile
{
Battery bt;
Mobile(Battery bt)
{
this .bt = bt;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
16. public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int percentage = sc.nextInt();
Battery bt = new Battery(percentage);
Mobile lenova = new Mobile(bt);
if(lenova.bt.percentage == 0)
{
System.out.print("Phone is switched Off");
}
else if((5 <= lenova.bt.percentage)&& (lenova.bt.percentage<=20) )
{
System.out.print("Please charge your phone");
}
else
{
System.out.print("No need of charging");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Section End/Start
Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).