cs-3443: add lab 1

This commit is contained in:
Price Hiller 2024-06-12 10:52:35 -05:00
parent 82212475d0
commit 33cf75a7c6
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
17 changed files with 853 additions and 0 deletions

View File

@ -0,0 +1,5 @@
.classpath
.settings/
target/
.metadata/
.project

View File

@ -0,0 +1,113 @@
*Objective:* Here, you'll have the opportunity to showcase your mastery of Java syntax, arrays, String class, OOP concepts, and class relationships. Embrace the challenge and let your understanding shine!
--------
*Estimated Time:* 3 hours
------
*Task:* Mr. John Hammond has asked for some basic software to help manage his theme park. (He says he will "spare no expense", but we'll see.) Version 1.0 of the software will create Java objects to represent the park itself and the dinosaurs within. Yes, dinosaurs. With this software, park operators will be able to plan each of the different types of dinosaurs the park contains as it develops and opens.
------
* Instructions
Begin by creating a new Java project in Eclipse, named according to the lab guidelines - project name should be "abc123-lab1", where abc123 is your UTSA ID.
Create the following new Java classes in the default package: (Use these names exactly!)
| Java Class | Comment |
|--------------------|----------------------------------------------------------|
| Park.java | |
| Dinosaur.java | This will be an interface |
| Theropod.java | This will be an abstract class, 1 of 3 types of Dinosaur |
| Sauropod.java | This will be an abstract class, 1 of 3 types of Dinosaur |
| Stegosaur.java | This will be an abstract class, 1 of 3 types of Dinosaur |
| Tyrannosaurus.java | This will be a class, 1 of 2 types of Theropod |
| Velociraptor.java | This will be a class, 1 of 2 types of Theropod |
| Apatosaurus.java | This will be a class, 1 of 2 types of Sauropod |
| Brachiosaurus.java | This will be a class, 1 of 2 types of Sauropod |
| Stegosaurus.java | This will be a class, 1 of 1 types of Stegosaur |
Note that we will have a few relationships between these classes. A Park contains ('has-a') Dinosaurs. There are 3 types of Dinosaurs: Theropods, Sauropods, and Stegosaurs. Velociraptor and Tyrannosaurus are types of Theropod. Apatosaurus and Brachiosaurus are types of Sauropod. Stegosaurus is a type of Stegosaur.
Place the given =Lab1.java= class in the default package within your project. =Lab1.java= has a main method and will be the class to run our application. Follow the remaining instructions for each class in this lab in order to get your code to compile - do not change the given class. Note that this code will not compile until you have completed the requirements of this lab. There will be syntax errors until all dependencies (classes and methods) are implemented. Once your code compiles, you will be able to examine the output of your program. The output of your program must match the format of the sample below. This sample is the result of running the =Lab1.java= class with the given main method.
#+begin_src
Welcome to Jurassic Park!
- - - - - - - - - - - - -
* Theropod: Velociraptor named Blue (carnivore)
* Theropod: Velociraptor named Delta (carnivore)
* Theropod: Velociraptor named Echo (carnivore)
* Theropod: Tyrannosaurus named Rex (carnivore)
* Sauropod: Apatosaurus named Littlefoot (not carnivore)
* Sauropod: Brachiosaurus named Bob (not carnivore)
* Stegosaur: Stegosaurus named Spike (not carnivore)
#+end_src
** More Details
*** Park.java
This class will represent a Park object, which we will define as having:
- A name, represented as a String (e.g: Jurassic Park)
- A max capacity of dinosaurs, represented as an int (e.g. in =Lab1.java=, the max capacity is set to $10$)
- Dinosaurs, stored as an array of Dinosaur objects (e.g. ~Dinosaur[]~)
- A constructor that initializes all instance variables and any other constructors as needed
- An ~addDino(...)~ method, which takes as a parameter a ~Dinosaur~ object and returns nothing
- A ~toString()~ method, which calls upon the ~toString()~ method in =Dinosaur.java= to return as a String all needed information (see the output above)
- Getters and setters for all variables
*** Dinosaur.java
This class will represent a Dinosaur, which we will define as an interface. This interface should declare the following methods (for the subclasses to implement):
- A method ~isVegetarian()~, which takes no parameters and returns a ~boolean~ for whether or not the dinosaur is a vegetarian (if not, they eat meat!)
- A ~getName()~ which takes no parameters and returns a String of the dinosaurs name (e.g. "Echo")
- A ~getType()~ method which takes no parameters and returns a String of the dinosaurs type (e.g. "Theropod - Velociraptor")
- A ~toString()~ method which returns a String representation of the dinosaur (see output above)
*** Theropod.java, Sauropod.java, Stegosaur.java
These classes will be abstract classes, representing the types of dinosaurs in our park. Each class should implement the Dinosaur interface and each should have:
- A name, represented as a String (i.e.: Rex)
- Whether or not the dino is a vegetarian - very important! This will be represented as a ~boolean~ (e.g. ~false~, for Rex, our tyrannosaurus)
- A constructor that initializes all instance variables, and any other constructors as needed
- A ~toString()~ method which returns a String representation of the dinosaur by calling upon "getter" methods.
- A ~getType()~ method, fulfilling the requirements of the super class.
- An abstract method ~getSubType()~ which takes no parameters and returns a sub type of dinosaur (e.g. "Velociraptor"). This method should be called by the ~getType()~ method.
*** Velociraptor.java, Tyrannosaurus.java, Apatosaurus.java, Brachiosaurus.java, Stegosaurus.java
These classes these will be very short classes in terms of amount of code. The classes will represent the specific types of dinosaurs which can be added to the park. Each class should extend its respective abstract class (see above for which classes should extend which abstract classes). In addition, they should have:
- A constructor that initializes all instance variables, and any other constructors as needed
- A ~getType()~ method which fulfills the requirements of the super class.
** Additional Notes
- Follow Java code style including the capitalization of your zip file, project name, and class names, and variables. (Recall that names of Java variables and methods should be in camel case)
- This lab is meant to review regular arrays in Java, no other data structure may be used to store the objects required. (No ArrayLists are permitted, for example).
- All classes should have Javadoc comments.
- The grader will test your code by modifying the main method in Lab1.java to add different dinos to a different park. If coded according to the requirements of this lab, your submission will output the correct result. However, if you "hard code" any portion of the lab (except Lab1.java, which again, you should not modify) your submission will fail this test case.)
** Submitting Your Lab:
- When you have finished programming, create a zip file of the project and submit it on Canvas
- right-click on your project in Eclipse
- Click "Export"
- Under "General" choose "Archive File"
- Ensure your project name is selected in the top left listing of projects (only the project you need to submit). Ensure all required files are included.
- Under "To archive file", select a place to save your file and name your zip file (for labs, follow the abc123-labX.zip convention)
- Upload only the zip file to Canvas (here)
/If you have any questions, feel free to reach out!/

View File

@ -0,0 +1,28 @@
* Lab 1 Submission
Name: =Price Hiller=
ABC123: =zfp106=
Date: =2024-06-12=
URL: [[https://git.orion-technologies.io/Price/college/src/branch/Development/Summer-2024/CS-3443/Labs/Lab1]]
** Running the program
Recording of the program running: [[file:./assets/run-recording.webm]]
*** via Eclipse
- Open Eclipse
- Select File
- Select Import
- In the dialogue that appears, expand =General=
- Select =Archive File=
- Click =Next=
- In the top right of the new menu, select =Browse...=
- Find the archive on your file system and select it
- Select a valid path for =Into folder:=
- Click =Finish=
- Select the project on the left bar
- On the top bar, expand the =Run= category
- Click =Run=
*** via Maven
If you have Maven installed, you can easily run this program when at the top level of the project via ~mvn exec:java -Dexec.mainClass="com.zfp106.lab1.Lab1"~.

View File

@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zfp106.lab1</groupId>
<artifactId>lab1</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>lab1</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.zfp106.lab1.Lab1</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,26 @@
package com.zfp106.lab1;
/**
* A concrete Apatosaurus implementation
*
*/
public class Apatosaurus extends Sauropod {
/**
* Create a new Apatosaurus
*
* @param name the name of the Apatosaurus
* @param isVegetarian whether or not the Apatosaurus is vegetarian
*/
public Apatosaurus(String name, Boolean isVegetarian) {
super(name, isVegetarian, "Apatosaurus");
}
/**
* Get the type of this Apatosaurus
*
* @return type of the Apatosaurus
*/
public String getType() {
return super.getType();
}
}

View File

@ -0,0 +1,26 @@
package com.zfp106.lab1;
/**
* A concrete Brachiosaurus implementation
*
*/
public class Brachiosaurus extends Sauropod {
/**
* Create a new Brachiosaurus
*
* @param name the name of Brachiosaurus
* @param isVegetarian whether or not the Brachiosaurus is vegetarian
*/
public Brachiosaurus(String name, Boolean isVegetarian) {
super(name, isVegetarian, "Brachiosaurus");
}
/**
* Get the type of this Brachiosaurus
*
* @return type of the Velociraptor
*/
public String getType() {
return super.getType();
}
}

View File

@ -0,0 +1,27 @@
package com.zfp106.lab1;
/**
* Base interface that ALL dinosaurs implement
*
*/
public interface Dinosaur {
/**
* @return whether or not the dinosaur is vegetarian
*/
public boolean isVegetarian();
/**
* @return name of the dinosaur
*/
public String getName();
/**
* @return the type of the dinosaur
*/
public String getType();
/**
* @return the string representation of the dinosaur
*/
public String toString();
}

View File

@ -0,0 +1,57 @@
package com.zfp106.lab1;
/**
* Lab1 is a Java class containing a main method to run your program when
* completed.
* This class will not compile until you have completed the requirements
* outlined in
* the lab description.
*
* @author Price Hiller (zfp106)
* UTSA CS 3443 - Lab 1
*
*/
public class Lab1 {
public static void main(String[] args) {
// create a Park object
Park jurassicPark = new Park("Jurassic Park", 10);
// All Parks contains Dinosaurs which come in different 3 types in our Jurassic
// Park: theropods, sauropods, & stegosaurs.
// create dinosaurs to add to the park
Theropod blue = new Velociraptor("Blue", false); // Velociraptors are a type of Theropod, which is a type of
// Dinosaur
Theropod delta = new Velociraptor("Delta", false); // Velociraptors are a type of Theropod, which is a type of
// Dinosaur
Theropod echo = new Velociraptor("Echo", false); // Velociraptors are a type of Theropod, which is a type of
// Dinosaur
Theropod rex = new Tyrannosaurus("Rex", false); // Tyrannosaurus are a type of Theropod, which is a type of
// Dinosaur
Sauropod littleFoot = new Apatosaurus("Littlefoot", true); // Apatosaurus are a type of Sauropod, which is a
// type of Dinosaur
Sauropod bob = new Brachiosaurus("Bob", true); // Brachiosaurus are a type of Sauropod, which is a type of
// Dinosaur
Stegosaur spike = new Stegosaurus("Spike", true); // Stegosaurus are a type of Stegosaur, which is a type of
// Dinosaur
// add all dinos to the park
jurassicPark.addDino(blue);
jurassicPark.addDino(delta);
jurassicPark.addDino(echo);
jurassicPark.addDino(rex);
jurassicPark.addDino(littleFoot);
jurassicPark.addDino(bob);
jurassicPark.addDino(spike);
// print the state of the park (see lab description)
System.out.println(jurassicPark);
}
}
// Fun Fact:
// Did you know? There are generally considered to be 7 types of dinos:
// theropods, sauropods, stegosaurs, ankylosaurs, ornithopods, ceratopsians, and
// pachycephalosaurs!

View File

@ -0,0 +1,106 @@
package com.zfp106.lab1;
/**
* A "Jurassic" Park. Like a zoo, but for dinosaurs.
*
* Allows holding collections of dinosaurs up to a set capacity and can have a name for the park.
*
*/
public class Park {
private String name;
private Integer capacity;
private Dinosaur[] dinosaurs;
/**
*
*
* @param name name of the park
* @param capacity how many dinosaurs the park can hold
*/
public Park(String name, Integer capacity) {
this.name = name;
this.capacity = capacity;
this.dinosaurs = new Dinosaur[this.capacity];
}
/**
* Get the string representation of the park, showing the name of the park and all dinosaurs
* within the park
*
* @return string representation of the park
*/
public String toString() {
String out = String.format("Welcome to %s!\n\n- - - - - - - - - - - - -", this.name);
for (Dinosaur dino : this.dinosaurs) {
if (dino != null) {
out = String.format("%s\n\n%s", out, dino.toString());
}
}
return out;
}
/**
* Add a dinosaur to the park, note that if you exceed the park capacity this
* will cause an
* error
*
* @param dino The dinosaur to add
*/
public void addDino(Dinosaur dino) {
for (int i = 0; i < dinosaurs.length; i++) {
// Instead of tracking how many dinosaurs are currently in the park, we can
// instead just check for an empty spot in the array. Less efficient, but I'm
// lazy 🤷
if (this.dinosaurs[i] == null) {
this.dinosaurs[i] = dino;
break;
}
}
}
/**
* Get the name of the park
*
* @return the name of the park
*/
public String getName() {
return name;
}
/**
* Sets the name of the park
*
* @param name The new name for the park
*/
public void setName(String name) {
this.name = name;
}
/**
* Get how many dinosaurs the park can hold
*
* @return the park's capacity
*/
public Integer getCapacity() {
return capacity;
}
/**
* Get all the dinosaurs in the park
*
* @return the dinosaurs in the park
*/
public Dinosaur[] getDinosaurs() {
return dinosaurs;
}
/**
* Set all the dinosaurs in the park
*
* @param dinosaurs all new dinosaurs for the park
*/
public void setDinosaurs(Dinosaur[] dinosaurs) {
this.capacity = dinosaurs.length;
this.dinosaurs = dinosaurs;
}
}

View File

@ -0,0 +1,97 @@
package com.zfp106.lab1;
/**
* Base class for all Sauropods
*
*/
public abstract class Sauropod implements Dinosaur {
private String name;
private boolean isVegetarian;
private String subType;
/**
* Construct a new Sauropod
*
* @param name the name of the Sauropod
* @param isVegetarian whether the Sauropod is vegetarian
* @param subtype the subType of the Sauropod, a specific dinosaur
*/
public Sauropod(String name, boolean isVegetarian, String subtype) {
this.setName(name);
this.setVegetarian(isVegetarian);
this.setSubType(subtype);
}
/**
* Get the string representation of the Sauropod
*
* @return the string representation of the Sauropod
*/
public String toString() {
return String.format("* %s named %s (%s)", this.getType(), this.getName(),
this.isVegetarian() ? "not carnivore" : "carnivore");
}
/**
* Get the name of the Sauropod
*
* @return the name of the Sauropod
*/
public String getName() {
return name;
}
/**
* Set the name of the Sauropod
*
* @param name the new name for the Sauropod
*/
public void setName(String name) {
this.name = name;
}
/**
* Get whether the Sauropod is vegetarian
*
* @return whether the Sauropod is vegetarian
*/
public boolean isVegetarian() {
return this.isVegetarian;
}
/**
* Set whether the Sauropod is vegetarian
*
* @param isVegetarian whether the Sauropod is vegetarian
*/
public void setVegetarian(boolean isVegetarian) {
this.isVegetarian = isVegetarian;
}
/**
* Get the type of the Sauropod with its subtype
*
* @return the type of the Sauropod
*/
public String getType() {
return "Sauropod: " + this.getSubType();
}
/**
* Get the subtype of the Sauropod, the specific Dinosaur
*
* @return the subtype of the Sauropod
*/
public String getSubType() {
return this.subType;
}
/**
* Set the subtype of the Sauropod
*
* @param subtype The new subtype for the Sauropod
*/
public void setSubType(String subtype) {
this.subType = subtype;
}
}

View File

@ -0,0 +1,97 @@
package com.zfp106.lab1;
/**
* Base class for all Stegosaurs
*
*/
public abstract class Stegosaur implements Dinosaur {
private String name;
private boolean isVegetarian;
private String subType;
/**
* Construct a new Stegosaur
*
* @param name the name of the Stegosaur
* @param isVegetarian whether the Stegosaur is vegetarian
* @param subtype the subType of the Stegosaur, a specific dinosaur
*/
public Stegosaur(String name, boolean isVegetarian, String subtype) {
this.setName(name);
this.setVegetarian(isVegetarian);
this.setSubType(subtype);
}
/**
* Get the string representation of the Stegosaur
*
* @return the string representation of the Stegosaur
*/
public String toString() {
return String.format("* %s named %s (%s)", this.getType(), this.getName(),
this.isVegetarian() ? "not carnivore" : "carnivore");
}
/**
* Get the name of the Stegosaur
*
* @return the name of the Stegosaur
*/
public String getName() {
return name;
}
/**
* Set the name of the Stegosaur
*
* @param name the new name for the Stegosaur
*/
public void setName(String name) {
this.name = name;
}
/**
* Get whether the Stegosaur is vegetarian
*
* @return whether the Stegosaur is vegetarian
*/
public boolean isVegetarian() {
return this.isVegetarian;
}
/**
* Set whether the Stegosaur is vegetarian
*
* @param isVegetarian whether the Stegosaur is vegetarian
*/
public void setVegetarian(boolean isVegetarian) {
this.isVegetarian = isVegetarian;
}
/**
* Get the type of the Stegosaur with its subtype
*
* @return the type of the Stegosaur
*/
public String getType() {
return "Stegosaur: " + this.getSubType();
}
/**
* Get the subtype of the Stegosaur, the specific Dinosaur
*
* @return the subtype of the Stegosaur
*/
public String getSubType() {
return this.subType;
}
/**
* Set the subtype of the Stegosaur
*
* @param subtype The new subtype for the Stegosaur
*/
public void setSubType(String subtype) {
this.subType = subtype;
}
}

View File

@ -0,0 +1,26 @@
package com.zfp106.lab1;
/**
* A concrete Stegosaurus implementation
*
*/
public class Stegosaurus extends Stegosaur {
/**
* Create a new Stegosaurus
*
* @param name the name of the Stegosaurus
* @param isVegetarian whether or not the Stegosaurus is vegetarian
*/
public Stegosaurus(String name, Boolean isVegetarian) {
super(name, isVegetarian, "Stegosaurus");
}
/**
* Get the type of this Stegosaurus
*
* @return type of the Stegosaurus
*/
public String getType() {
return super.getType();
}
}

View File

@ -0,0 +1,97 @@
package com.zfp106.lab1;
/**
* Base class for all Theropods
*
*/
public abstract class Theropod implements Dinosaur {
private String name;
private boolean isVegetarian;
private String subType;
/**
* Construct a new Theropod
*
* @param name the name of the Theropod
* @param isVegetarian whether the Theropod is vegetarian
* @param subtype the subType of the Theropod, a specific dinosaur
*/
public Theropod(String name, boolean isVegetarian, String subtype) {
this.setName(name);
this.setVegetarian(isVegetarian);
this.setSubType(subtype);
}
/**
* Get the string representation of the Theropod
*
* @return the string representation of the Theropod
*/
public String toString() {
return String.format("* %s named %s (%s)", this.getType(), this.getName(),
this.isVegetarian() ? "not carnivore" : "carnivore");
}
/**
* Get the name of the Theropod
*
* @return the name of the Theropod
*/
public String getName() {
return name;
}
/**
* Set the name of the Theropod
*
* @param name the new name for the Theropod
*/
public void setName(String name) {
this.name = name;
}
/**
* Get whether the Theropod is vegetarian
*
* @return whether the Theropod is vegetarian
*/
public boolean isVegetarian() {
return this.isVegetarian;
}
/**
* Set whether the Theropod is vegetarian
*
* @param isVegetarian whether the Theropod is vegetarian
*/
public void setVegetarian(boolean isVegetarian) {
this.isVegetarian = isVegetarian;
}
/**
* Get the type of the Theropod with its subtype
*
* @return the type of the Theropod
*/
public String getType() {
return "Theropod: " + this.getSubType();
}
/**
* Get the subtype of the Theropod, the specific Dinosaur
*
* @return the subtype of the Theropod
*/
public String getSubType() {
return this.subType;
}
/**
* Set the subtype of the Theropod
*
* @param subtype The new subtype for the Theropod
*/
public void setSubType(String subtype) {
this.subType = subtype;
}
}

View File

@ -0,0 +1,26 @@
package com.zfp106.lab1;
/**
* A concrete Tyrannosaurus implementation
*
*/
public class Tyrannosaurus extends Theropod {
/**
* Create a new Tyrannosaurus
*
* @param name the name of the Tyrannosaurus
* @param isVegetarian whether or not the Tyrannosaurus is vegetarian
*/
public Tyrannosaurus(String name, Boolean isVegetarian) {
super(name, isVegetarian, "Tyrannosaurus");
}
/**
* Get the type of this Tyrannosaurus
*
* @return type of the Tyrannosaurus
*/
public String getType() {
return super.getType();
}
}

View File

@ -0,0 +1,26 @@
package com.zfp106.lab1;
/**
* A concrete Velociraptor implementation
*
*/
public class Velociraptor extends Theropod {
/**
* Create a new Velociraptor
*
* @param name the name of the Velociraptor
* @param isVegetarian whether or not the Velociraptor is vegetarian
*/
public Velociraptor(String name, Boolean isVegetarian) {
super(name, isVegetarian, "Velociraptor");
}
/**
* Get the type of this Velociraptor
*
* @return type of the Velociraptor
*/
public String getType() {
return super.getType();
}
}

View File

@ -0,0 +1,49 @@
package com.zfp106.lab1;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class ParkTests {
@Test
void parkStringReprIsCorrect() {
Park jurassicPark = new Park("Jurassic Park", 10);
Theropod blue = new Velociraptor("Blue", false);
Theropod delta = new Velociraptor("Delta", false);
Theropod echo = new Velociraptor("Echo", false);
Theropod rex = new Tyrannosaurus("Rex", false);
Sauropod littleFoot = new Apatosaurus("Littlefoot", true);
Sauropod bob = new Brachiosaurus("Bob", true);
Stegosaur spike = new Stegosaurus("Spike", true);
jurassicPark.addDino(blue);
jurassicPark.addDino(delta);
jurassicPark.addDino(echo);
jurassicPark.addDino(rex);
jurassicPark.addDino(littleFoot);
jurassicPark.addDino(bob);
jurassicPark.addDino(spike);
String expected = "Welcome to Jurassic Park!\n" +
"\n" +
"- - - - - - - - - - - - -\n" +
"\n" +
"* Theropod: Velociraptor named Blue (carnivore)\n" +
"\n" +
"* Theropod: Velociraptor named Delta (carnivore)\n" +
"\n" +
"* Theropod: Velociraptor named Echo (carnivore)\n" +
"\n" +
"* Theropod: Tyrannosaurus named Rex (carnivore)\n" +
"\n" +
"* Sauropod: Apatosaurus named Littlefoot (not carnivore)\n" +
"\n" +
"* Sauropod: Brachiosaurus named Bob (not carnivore)\n" +
"\n" +
"* Stegosaur: Stegosaurus named Spike (not carnivore)";
assertEquals(expected, jurassicPark.toString());
}
}