Professional Documents
Culture Documents
OBJECTIVES
✓ Apply the principles of Abstraction and Interfaces in Java
✓ Implement UML design in OOP
INSTRUCTION:
• Provide the Java code as your analysis and solution of the following problems.
• Upload your Java Syntax in a text editor, or word document.
• Yung submitted Java Syntax will have actual testing and checking which will happened during face to face
laboratory class.
1. The following (Name, Race, Stats) are the information needed for character creation in a RPG game. The
following are abstract classes to set the character details. Display the details of the character in the main
program. Provide the UML diagram. There are two players who will create a character.
• NAME() – accepts the in-game character of the player as an input and return it.
• RACE() – accepts the race of the character of the player as an input and return it. The following are
the choices. Ask the player again if it is a wrong input.
1 – Human 4 – Angel
2 – Elf 5 – Demon
3 – Orc
• STATS() – accepts the three initial stats of the character (HP, DEF, ATK) and return it as an array
[HP,DEF,ATK]. In every game, the initial stats points available are limited for the player to distribute
on the character. The program will ask again for new input if it exceeds 10 total stat points.
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 2
CEAT – Engineering Department
if (race == 1){
return "Human";
} else if (race == 2) {
return "Elf";
} else if (race == 3) {
return "Orc";
} else if (race == 4) {
return "Angel";
} else {
return "Demon";
}
}
Main Class
public class Main {
System.out.print("----------------------------------");
System.out.println("\n\tPLAYER 1 DETAILS:");
System.out.println("Name: " + name);
System.out.println("Race: " + race);
System.out.println("HP: " + stats[0]);
System.out.println("DEF: " + stats[1]);
System.out.println("ATK: " + stats[2]);
System.out.println("\n\tPLAYER 2 DETAILS:");
System.out.println("Name: " + name1);
System.out.println("Race: " + race1);
System.out.println("HP: " + stats1[0]);
System.out.println("DEF: " + stats1[1]);
System.out.println("ATK: " + stats1[2]);
}
}
Output
CREATE PLAYER 1:
What's your character name? Sean
What's your race?
1 - Human 4 - Angel
2 - Elf 5 - Demon
3 - Orc
Answer: 3
Assign 10 pts to stats:
HP: 2
DEF: 3
ATK: 5
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 4
CEAT – Engineering Department
CREATE PLAYER 2:
What's your character name? Ulric
What's your race?
1 - Human 4 - Angel
2 - Elf 5 - Demon
3 - Orc
Answer: 4
Assign 10 pts to stats:
HP: 3
DEF: 3
ATK: 4
----------------------------------
PLAYER 1 DETAILS:
Name: Sean
Race: Orc
HP: 2
DEF: 3
ATK: 5
PLAYER 2 DETAILS:
Name: Ulric
Race: Angel
HP: 3
DEF: 3
ATK: 4
UML Diagram
Character
+ race : int
+ hp : int
+ def : int
+ atk : int
+ total : int
+ Name() : String
+ Race() : String
+ Stats() : int[]
extends
CharacterChild
+ Name() : String
+ Race() : String
+ Stats() : int[]
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 5
CEAT – Engineering Department
2. Create three interfaces for the following. Provide the UML diagram of your program.
• Method that accepts the number and return if its positive or negative, its square and its cube.
• Methods that accept 2 parameters and return their difference, product and quotient.
• Methods that accept 2 to 4 parameters and return their sum. (Use overloaded)
PosNeg Interface
public interface PosNeg {
public abstract String PosOrNeg(int A);
public abstract int Squared(int A);
public abstract int Cubed(int A);
}
DiffProdQuot Interface
public interface DiffProdQuot {
public abstract int Difference(int A, int B);
public abstract int Product(int A, int B);
public abstract double Quotient(int A, int B);
}
Sum Interface
public interface Sum {
public abstract int Summed(int A, int B);
public abstract int Summed(int A, int B, int C);
public abstract int Summed(int A, int B, int C, int D);
}
AcceptAll Class
public class AcceptAll implements PosNeg, DiffProdQuot, Sum{
Main Class
public class Main {
Output
Sign: Positive
Sign: Negative
Squared: 144
Cubed: 27
Difference: 66
Product: 99
Quotient: 6.25
Sum of 2: 30
Sum of 3: 60
Sum of 4: 100
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 7
CEAT – Engineering Department
UML Diagram
<<interface>>
PosNeg
+ PosOrNeg(int) : String
+ Squared(int) : int implements
+ Cubed(int) : int
<<interface>> AcceptAll
DiffProdQuot
implements
+ Difference(int, int) : int + PosOrNeg(int) : String
+ Product(int, int) : int + Squared(int) : int
+ Quotient(int, int) : double + Cubed(int) : int
implements + Difference(int, int) : int
+ Product(int, int) : int
<<interface>> + Quotient(int, int) : double
Sum + Summed(int, int) : int
+ Summed(int, int, int) : int
+ Summed(int, int) : int + Summed (int, int, int, int) : int
+ Summed(int, int, int) : int
+ Summed (int, int, int, int) : int
3. The following are the description of the variables in an abstract class. Apply
encapsulation and abstraction to set and get the values of these variables. Display
these data (for three countries CountryA, CountryB, CountryC) in the main
program. Complete the UML diagram.
• Country - country name
• population - number of populations
• infected - number of infected
• recovered - number of recovered
• ratioOfInfected – string ratio per one infected people. Example: A
country has 1000 population with 10 infected. For every 100 people, 1 is
infected. That is 1:100.
if (A % i == 0 && B % i == 0) {
gcf = i;
}
}
ratioOfInfected = A/gcf + ":" + B/gcf;
}
Main Class
public class Main {
// COUNTRY A
System.out.println("\tCountry A:");
CountryA.setCountry("Philippines");
System.out.println("Country: " + CountryA.getCountry());
CountryA.setPopulation(111000000);
System.out.println("Population: " + CountryA.getPopulation());
CountryA.setInfected(4050000);
System.out.println("Infected: " + CountryA.getInfected());
CountryA.setRecovered(3900000);
System.out.println("Recovered: " + CountryA.getRecovered());
CountryA.setRatio(CountryA.getInfected(),
CountryA.getPopulation());
System.out.println("Ratio of Infected: " + CountryA.getRatio());
// COUNTRY B
System.out.println("\n\tCountry B:");
CountryB.setCountry("Japan");
System.out.println("Country: " + CountryB.getCountry());
CountryB.setPopulation(125700000);
System.out.println("Population: " + CountryB.getPopulation());
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 10
CEAT – Engineering Department
CountryB.setInfected(26500000);
System.out.println("Infected: " + CountryB.getInfected());
CountryB.setRecovered(20860000);
System.out.println("Recovered: " + CountryB.getRecovered());
CountryB.setRatio(CountryB.getInfected(),
CountryB.getPopulation());
System.out.println("Ratio of Infected: " + CountryB.getRatio());
// COUNTRY C
System.out.println("\n\tCountry C:");
CountryC.setCountry("USA");
System.out.println("Country: " + CountryC.getCountry());
CountryC.setPopulation(331900000);
System.out.println("Population: " + CountryC.getPopulation());
CountryC.setInfected(101400000);
System.out.println("Infected: " + CountryC.getInfected());
CountryC.setRecovered(98600000);
System.out.println("Recovered: " + CountryC.getRecovered());
CountryC.setRatio(CountryC.getInfected(),
CountryC.getPopulation());
System.out.println("Ratio of Infected: " + CountryC.getRatio());
}
}
Output
Country A:
Country: Philippines
Population: 111000000
Infected: 4050000
Recovered: 3900000
Ratio of Infected: 27:740
Country B:
Country: Japan
Population: 125700000
Infected: 26500000
Recovered: 20860000
Ratio of Infected: 265:1257
Country C:
Country: USA
Population: 331900000
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 11
CEAT – Engineering Department
Infected: 101400000
Recovered: 98600000
Ratio of Infected: 1014:3319
extends
CASE
- gcf : int
- smaller : int
+ setCountry(String) : void
+ getCountry() : String
+ setPopulation(int) : void
+ getPopulation() : int
+ setInfected(int) : void
+ getInfected() : int
+ setRecovered(int) : void
+ getRecovered() : int
+ setRatio(int, int) : void
+ getRatio() : String
T-CPET221LA
Object Oriented Programming