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
De La Salle University - Dasmariñas 20
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
T-
CPET221LA
De La Salle University - Dasmariñas 40
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
De La Salle University - Dasmariñas 50
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 {
DiffProdQuot
publicInterface
abstract String PosOrNeg(int
publicA); public abstract
interface int Squared(int
DiffProdQuot {
Sum Interface
public abstract
A); public int Difference(int
abstract A, int B);
int Cubed(int A);
}
publicpublic abstract
interface int Product(int A, int B);
Sum {
AcceptAll Classabstract
public abstract double
int Summed(int A, int
Quotient(int B);
A, int
public classabstract
public
B); AcceptAll implements
int PosNeg,
Summed(int A, intDiffProdQuot,
B, int Sum{
C);
public abstract
int Summed(int A, int B)A, int B, int C, int D);
int Summed(int
{ return A + B;
}
T-
CPET221LA
De La Salle University - Dasmariñas 60
CEAT – Engineering Department
Main Class
public class Main {
Output
Sign:public static void main(String[] args)
Positive { AcceptAll objA = new
Sign: AcceptAll();
Negative System.out.println("Sign: " + objA.PosOrNeg(10));
System.out.println("Sign: " + objA.PosOrNeg(-10));
Squared: 144
Cubed: 27 System.out.println("Squared: " + objA.Squared(12));
System.out.println("Cubed:
Difference: 66 " + objA.Cubed(3));
System.out.println("Difference:
Product: 99 " + objA.Difference(99,
33)); System.out.println("Product: " + objA.Product(9,
Quotient: 6.25
11)); System.out.println("Quotient: " + objA.Quotient(25,
Sum of 2: 30
4)); System.out.println("Sum of 2: " + objA.Summed(10,
20)); System.out.println("Sum of 3: " + objA.Summed(10, 20,
30));
System.out.println("Sum of 4: " + objA.Summed(10, 20, 30, 40));
T-
CPET221LA
De La Salle University - Dasmariñas 70
CEAT – Engineering Department
UML Diagram
<<interface>>
PosNeg
+ PosOrNeg(int) : String
implements
+ Squared(int) : int
+ Cubed(int) : int
<<interface>> AcceptAll
DiffProdQuot implements
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.
T-
CPET221LA
De La Salle University - Dasmariñas 80
CEAT – Engineering Department
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
De La Salle University - Dasmariñas
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
De La Salle University - Dasmariñas
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