We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
University of Tlemcen 21/Semester1 07/12/2023
College of Sciences 7
Department of Computer Sciences Ah
Lab test: Object Oriented Programming 1 - Duration: 1h ‘e
Student Name:
Group:
Exereise 1 (5 marks):
Given the following java code, run it manuelly and display the different results:
‘The Traffie,java file
class TrafficLightf
private int color; private int position ;
private static double height = 9.0;
Public TrafficLighi(intintColor) { color = initColor; position = 0 ;}
public TrafficLight(ine initColor, int initPosition) {color = initColor; position = initPosition ;}
‘public static vold setHetght(double newHeight) {height = newHleight;}
public void changeColor (int newColor) {color = newColor:System.out.printin("My color is : “+ color):}
‘Public static void getFeight() (System. out.printin("My height is :"+ height);}
public void flash@) {
‘System. cut println( "First way to flash’), Results
Jor (int a=0; a€2; a++) { ‘Nefcolor is 2) oS
‘ar (int b=0; <2; b+) y color is : vé
‘System.out.printin("T am off”) My height is 72
Jor (int b=0; b<3; b+) My height is : a2
‘Sysiem.out.printin("T am on"):)} Second way to flash 0, 5
i vlan a ai Tantr, / R A
stem. out,priniln("Second way to flash"); = rs ”
for (Int a0; 082; a+) { Tamon FO US Cac
“Jor (int b=0; b")=
‘public void translate (int dx, int dy, Ine de) fx +e:
Sstem.out printin("%="+x4" y="tys" 2m
public class TestPoint {
ublie static void Main) (Point p! ~ new Point (5):Point p2 = new Point (6, 7);Point p3 = new Point (4, 3, 6):
pltranslate(2); p2.translate(3,4); p3.translate(1,2.4):}}
Exereise 3 (3 marks):
Given a class called Account, which models 2 bank account of a customer, as shown in the following java code. The
methods credit(amount) and debit(amount) add or subtract the given amount to the balance, The method transferTo (another,
smount) transfers the given amount from this Account tothe given another Account.
‘Complete the missing part in the body of the transferTo method.
public class Account {
private String id;
private String name;
private int balance ~ 0:
‘public Account (String id, String name) {
this id = ia
this.name = name;
this.balance = 0;}
public Account (String id, String name, int balance) {
this.id =
this.name = name;
this.balance = batance;}
public String gettd0 { return id: }
public String getName() { return name; }
Public im getBalance() { return balance; }
public void setBalance(int balance) { this.balance = balance; }
‘public ini credit (int amount) { return tis.balance + amount; }
public int debit (int amount) {
if (amount < this.balance)
this.balance = this. balance - amount;
alse
‘System.cut priniin ("Amount exceeded balance’);
return this.balance; }
public int iransferTo (Account another, int amount) {
int bal:
if (amount < this. balance) Answers (Missing part of the code)
this.balance = this.balance - amount; |}. a wa
‘bal = another. getBalance() + amount; 2 )
eae
another. setBalance(bal);
+ this.name +" BALANCE" + this balance +")":}}
Mayr teamea)i)
‘System.out prinila("Amount transferred");}
else
‘System. our.priniln("Amount exceeded balance")
return this.balance; }
public String toString) return "Account[ID= "+ this.id + " NAME
Exereise 4 (3 marks): naenees -
The following java code will aot compile, Why ? Bouse dete hes private access in the cass Calendar A?
explain why and correct the code.
‘The Calendar, java file
public class Calendar {
Li ey tudae dan) | Steen ereidanony Le TS a
‘public String readDate () {return ""*dace;}} vw
The Test.java file
public class Test (
public static void main( Sering[] args) (
Calendar ¢ = new Calendar (7122023);
‘Shsiem.outprintin (e.date):3}
Corrected part of the code: