0% found this document useful (0 votes)
11 views9 pages

Lab2 ProgamacionII

This document discusses exercises exploring encapsulation and creating Java packages. It describes modifying an Account class to make its fields private and add access methods, moving the Account and TestAccount classes to packages, and compiling and running the updated TestAccount class.

Uploaded by

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

Lab2 ProgamacionII

This document discusses exercises exploring encapsulation and creating Java packages. It describes modifying an Account class to make its fields private and add access methods, moving the Account and TestAccount classes to packages, and compiling and running the updated TestAccount class.

Uploaded by

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

INF514. Lenguaje de Programación 2.

Exercise 2: Exploring Encapsulation, Version 1

“Task 1 – Deleting the Account Class”

“Task 2 – Creating the Account Class”

Diagrama UML de la clase Account:

Código Fuente:

public class Account{


public double balance;

public Account(double initBalance){


balance = initBalance;
}
}

“Task 3 – Creating the TestAccount2 Class”

Código Fuente:

public class TestAccount2{


public static void main (String [] args){
Account acc = new Account(100.0);

acc.balance = acc.balance + 47.0;


acc.balance = acc.balance - 150.0;

System.out.println("Balance: " + acc.balance);


}
}
“Task 4 – Compiling the TestAccount2 Class”

“Task 5 – Running the TestAccount2 Program”


Exercise 3: Exploring Encapsulation, Version 2

“Task 1 – Modifying the Account Class”

public class Account{


private double balance;

public Account(double initBalance){


balance = initBalance;
}

public double getBalance(){


return balance;
}

public void deposit(double amt){


balance = balance + amt;
}

public void withdraw(double amt){


if ((balance - amt) <= 0) {
System.out.println("Balance insuficiente");
} else {
balance = balance - amt;
}
}
}
“Task 2 – Modifying the TestAccount Class”

public class TestAccount{


public static void main(String [] args){
Account ac = new Account(100.0);
ac.deposit(47.0);
ac.withdraw(150.0);
System.out.println("Balance: " + ac.getBalance());
}
}

“Task 3 – Compiling the TestAccount Class”

“Task 4 – Running the TestAccount Program”


Exercise 4: Creating Java Packages

“Task 1 – Creating the Java Packages”


“Task 2 – Moving and Modifying the Account Class”

package com.mybank.domain;

public class Account{


private double balance;

public Account(double initBalance){


balance = initBalance;
}

public double getBalance(){


return balance;
}

public void deposit(double amt){


balance = balance + amt;
}

public void withdraw(double amt){


if ((balance - amt) <= 0) {
System.out.println("Balance insuficiente");
} else {
balance = balance - amt;
}
}
}
“Task 3 – Moving the TestAccount Class”

package com.mybank.test;

public class TestAccount{


public static void main(String [] args){
Account ac = new Account(100.0);
ac.deposit(47.0);
ac.withdraw(150.0);
System.out.println("Balance: " + ac.getBalance());
}
}

“Task 4 – Compiling the TestAccount Class”


“Task 5 – Running the TestAccount Program”

You might also like