You are on page 1of 11

De La Salle University - Dasmariñas 1

CEAT – Engineering Department

T-CPET221LA – Object Oriented Programming


ENABLING ASSESSMENT 7: Abstraction and Interfaces
Name: Sean Ulric C. Montano
Year and Section: CPE-21 Date: Dec 15, 2022 Rating:

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.

Character Abstract Class (Parent Class)


import java.util.Scanner;

public abstract class Character {


Scanner in = new Scanner (System.in);
public int race, hp, def, atk, total;
public int[] stats = new int[3];

public abstract String Name();


public abstract String Race();
public abstract int[] Stats();
}

T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 2
CEAT – Engineering Department

CharacterChild Class (Child Class)


public class CharacterChild extends Character{

public String Name() {


System.out.print("What's your character name? ");
return in.nextLine();
}

public String Race() {


do {
System.out.print("What's your race?\n1 - Human\t4 -
Angel\n2 - Elf\t\t5 - Demon\n3 - Orc\nAnswer: ");
race = in.nextInt();
} while (race < 1 || race > 5);

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";
}
}

public int[] Stats() {


do {
System.out.println("Assign 10 pts to stats: ");
System.out.print("HP: ");
hp = in.nextInt();
System.out.print("DEF: ");
def = in.nextInt();
System.out.print("ATK: ");
atk = in.nextInt();
total = hp + def + atk;
if (total > 10) {
System.out.println("Cannot exceed 10 pts");
}
} while (total > 10);
this.stats[0] = hp;
this.stats[1] = def;
this.stats[2] = atk;
return stats;
}
}
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 3
CEAT – Engineering Department

Main Class
public class Main {

public static void main(String[] args) {


CharacterChild player1 = new CharacterChild();
CharacterChild player2 = new CharacterChild();

System.out.println("\tCREATE PLAYER 1:");


String name = player1.Name();
String race = player1.Race();
int[] stats = player1.Stats();

System.out.println("\n\tCREATE PLAYER 2:");


String name1 = player2.Name();
String race1 = player2.Race();
int[] stats1 = player2.Stats();

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{

public int Summed(int A, int B) {


return A + B;
}

public int Summed(int A, int B, int C) {


return A + B + C;
}

public int Summed(int A, int B, int C, int D) {


return A + B + C + D;
}

public int Difference(int A, int B) {


return A - B;
}

public int Product(int A, int B) {


return A * B;
}
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 6
CEAT – Engineering Department

public double Quotient(int A, int B) {


return (double)A / B;
}

public String PosOrNeg(int A) {


return A > 0 ? "Positive":"Negative";
}

public int Squared(int A) {


return A*A;
}

public int Cubed(int A) {


return A*A*A;
}
}

Main Class
public class Main {

public static void main(String[] args) {


AcceptAll objA = new AcceptAll();
System.out.println("Sign: " + objA.PosOrNeg(10));
System.out.println("Sign: " + objA.PosOrNeg(-10));
System.out.println("Squared: " + objA.Squared(12));
System.out.println("Cubed: " + objA.Cubed(3));
System.out.println("Difference: " + objA.Difference(99, 33));
System.out.println("Product: " + objA.Product(9, 11));
System.out.println("Quotient: " + objA.Quotient(25, 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));
}
}

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.

NCOV Abstract Class (Parent Class)


public abstract class NCOV {
public String Country, ratioOfInfected;
public int population, infected, recovered;

public abstract void setCountry(String A);


public abstract String getCountry();

public abstract void setPopulation(int A);


public abstract int getPopulation();

public abstract void setInfected(int A);


public abstract int getInfected();
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 8
CEAT – Engineering Department

public abstract void setRecovered(int A);


public abstract int getRecovered();

public abstract void setRatio(int A, int B);


public abstract String getRatio();
}

Case Class (Child Class)


public class CASE extends NCOV{
private int gcf, smaller;

public void setCountry(String A) {


Country = A;
}

public String getCountry() {


return Country;
}

public void setPopulation(int A) {


population = A;
}

public int getPopulation() {


return population;
}

public void setInfected(int A) {


infected = A;
}

public int getInfected() {


return infected;
}

public void setRecovered(int A) {


recovered = A;
}

public int getRecovered() {


return recovered;
}

public void setRatio(int A, int B) {


gcf = 1;
smaller = Math.min(A, B);
for (int i = 1; i <= smaller; i++) {
T-CPET221LA
Object Oriented Programming
De La Salle University - Dasmariñas 9
CEAT – Engineering Department

if (A % i == 0 && B % i == 0) {
gcf = i;
}
}
ratioOfInfected = A/gcf + ":" + B/gcf;
}

public String getRatio() {


return ratioOfInfected;
}
}

Main Class
public class Main {

public static void main(String[] args) {


CASE CountryA = new CASE();
CASE CountryB = new CASE();
CASE CountryC = new CASE();

// 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

Completed UML Diagram


NCOV
+ Country : String
+ population : int
+ infected : int
+ recovered: int
+ ratioOfInfected : String
+ 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

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

You might also like