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

Oop Lab Session 6

The document discusses a lab session assignment on encapsulation in Java where students are asked to create a class to represent course sections with fields like course ID, number of registered students, and maximum allowed, and provide methods to update registration counts; it also provides examples of creating classes for flights and validating number properties.

Uploaded by

maryam raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Oop Lab Session 6

The document discusses a lab session assignment on encapsulation in Java where students are asked to create a class to represent course sections with fields like course ID, number of registered students, and maximum allowed, and provide methods to update registration counts; it also provides examples of creating classes for flights and validating number properties.

Uploaded by

maryam raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB SESSION 6: ENCAPSULATION IN JAVA

Student Name : Maryam Raza


Depart : BSCS Section A

Q1) You are required to design a class that represents sections of courses in university. Each
section has a number that represents the course ID, the number of students registered in the
section, and a maximum number of registered students allowed. All sections should have the
same maximum number of registered students allowed. All data fields should be
encapsulated. In addition, your class should provide methods to update the number of
students registered when a new student is registered and when a student drops the course.

SOURCE CODE
public class Lab6 {

private int max_allowed=15; private int ID1=123;

private int reg_Students1=10; private int ID2=234;

private int reg_Students2=5; private int ID3=345;

private int reg_Students3=11;

public int registered1(){ if(reg_Students1!=15){ int r1; r1=reg_Students1+=1; return r1;}

else{

System.out.println("no seats left in this section/course")}} public int drop1(){

int d1; d1=reg_Students1-=1; return d1;}

public int registered2(){ if(reg_Students2!=15){ int r2;

r2=reg_Students2+=1; return r2;}

else{
System.out.println("no seats left in this section/course")}} public int drop2(){

int d2; d2=reg_Students2-=1; return d2;}

public int registered3(){ if(reg_Students2!=15){ int r3; r3=reg_Students3+=1; return r3;}

else{

System.out.println("no seats left in this section/course")}} public int drop3(){

int d3; d3=reg_Students3-=1; return d3;}}

public class testLab6 {

public static void main(String[] args){ Lab6 obj=new Lab6();

obj.drop3(); System.out.println(obj.reg_Students3); obj.registered2();


System.out.println(obj.reg_Students2);

OUTPUT

Q2) For this problem you will complete a class named Flight. public class Flight {

private int flightNumber; private String captain; private String[] seats;

private int numberOfPassengers;

// Methods you will implement go here

For this problem:

All the methods are public and non-static.


Constructor: It has three parameters (flight number, captain’s name, and maximum number
of seats) that are used to initialize the object. The maximum number of seats represents the
size of the seats array that will be created by the constructor.

• Default Constructor: It initializes the object using the values 2, “STAFF”, and 10.

You must use “this” to call the previous constructor.

• addPassenger: Takes a string representing a passenger’s name. It adds the


passenger to the end of the seats array, if there is space.

• The sample output below can help you verify the functionality of some of the

methods described above.

o Flight Number: 132

o Captain: Nerdson

o Maximum Number Seats: 5

o Passengers:

o Yulu

o Justin

SOURCE CODE
package flight1; public class Flight1 {

private final int flightNumber; private final String captain;

private final String [] seats; private int numberOfPassengers; String p_name;

String s_name; String k_name; String l_name; String m_name;

Flight1(int f_num,String c_name,int max_seats){ flightNumber=f_num;

captain=c_name;

seats=new String[max_seats];} Flight1(){

this(2,"STAFF",10);
}

void addPassenger(String PN,String pn, String kn,String ln, String mn){ p_name=PN;

seats[0]=p_name; s_name=pn; seats[1]=s_name; k_name=kn; seats[2]=k_name; l_name=ln;


seats[3]=l_name; m_name=mn; seats[4]=m_name;

public int getNO(){ return flightNumber;} public String getName(){ return captain;}

public int getSeats(){ return seats.length;}

public static void main(String[] args) { Flight1 obj=new Flight1(132,"Nerdson",5);

obj.addPassenger("Bakhtawar","Sana","Jaweria","Ummul Qura","none");
System.out.println("Flight Number: "+obj.getNO()); System.out.println("Captain:
"+obj.getName()); System.out.println("Maximum number of seats: "+obj.getSeats());
System.out.println("passengers: ");

for(String seat:obj.seats){ System.out.println(seat);}}

OUTPUT

Q3) Create a class Number with only one private instance variable as a double primitive type.
Include a default constructor which initializes an instance variable to 0.0. The class should
have the following methods:

• isZero( )

• isPositive()
• isNegative( )

• isOdd( )

• isEven( )

The above methods return Boolean primitive type. Write a driver class NumberTest to test
constructor and each method in the class.

SOURCE CODE
public class Number { double num;

boolean isZero(double n){ num=n;

if(num==0){ return true;} else{

return false;}}

boolean isPositive(double n){ num=n;

if(num>0){

return true;} else{

return false;}}

boolean isNegative(double n){ num=n;

if(num<0){ return true;} else{

return false;}}

boolean isOdd(double n){ num=n;

if(num%2!=0){ return true;} else{

return false;}}

boolean isEven(double n){ num=n;

if(num%2==0){ return true;} else{

return false;}}}
public class NumberTest {

public static void main(String[] args) { Number obj=new Number();

System.out.println(obj.isZero(0)); System.out.println(obj.isPositive(-2));
System.out.println(obj.isNegative(-4)); System.out.println(obj.isOdd(4));

System.out.println(obj.isEven(6));}}

OUTPUT

You might also like