Homework 6
Part A
Answer the following in a Word or .txt document. Attach this to the same submission as for part B
- Discuss how designing a program in an object oriented paradigm would be different than in a procedural paradigm.
Include a brief definition of each paradigm and discuss advantages and disadvantages of each.
- In a game, owls carry messages between wizard towers. The messages could have treasure or
dangerous spells in them that make your tower explode, so you have to think about how good your owl is at carrying messages and about the defenses
of your tower. Create an object oriented design for this game, including names for classes, characteristics, and behavior
methods you think should be included. You are only being asked for the design; you do NOT need to write any code. (There
is more than one good answer, your design should show me that you thought through the situation in an object oriented way)
Part B
The following example code shows using classes in java. Note that in Java
- ALL variables based on class types are pointers: you can't have just a Dog, you can only
have a pointer to a Dog
- primitive types in java CANNOT have pointers, so you cannot have a pointer to an int or double
- So: java does not need the * syntax to distinguish between pointers and non-pointers
- In Java, different classes should live in different files, each named with the class name and .java
// this file is Dog.java
// Dog class, no public keyword
public class Dog {
//instance variables (public for our simple code)
public String name;
public int age;
public boolean awake;
public Dog bestFriend; // ALL class variables in java are pointers, no * needed
public Cat buddy;
// behavior methods:
// dog barks
public void bark() {
System.out.println(name + " says woof");
// check if we have a best friend
// and if they were sleeping when we barked
if (bestFriend != null && !bestFriend.awake) {
System.out.println(name + " woke up their best friend " + bestFriend.name);
bestFriend.awake = true;
}
}
// dog goes to sleep
public void sleep() {
System.out.println(name + " goes to sleep");
awake = false;
}
} // Dog class ends here
// this file is Cat.java
// Cat class
public class Cat {
public String name;
public void miaow() {
System.out.println(name + " says miaow");
}
}// end of Cat class
// this file is Main.java
public class Main {
public static void main(String[] args) {
Dog fluffy = new Dog(); // note parentheses when using new in Java
fluffy.name = "Fluffywuffykins";
Dog muffy = new Dog();
muffy.name = "Miss Muffet";
// all Dog variables in Java are pointers to Dogs
// so this points at muffy, not making a copy
fluffy.bestFriend = muffy;
fluffy.bestFriend.sleep();
fluffy.bark();
// all Cat variables in java are pointers to Cats
fluffy.buddy = new Cat();
fluffy.buddy.name = "Ginger";
fluffy.buddy.miaow();
}
} // end of Main class
-
Create a class representing a Grade, which has instance variables that are doubles to store the classwork grade, the homework grade, and a test grade. Give this class a method computeFinalGrade that first prints each of the instance vars, with labels, then averages the three parts of the grade and prints the result.
-
Create a class representing a Student, which has instance variables that are Strings for the first and last name of the student, and a Grade instance variable to hold the student's grade. Give the student a method aceTest that sets their test grade to 100 and reports that they aced it.
-
Give Student another instance variable called partner to store another Student. Also give Student a method markClasswork --
If they have a partner (that is, the variable is not null) then set both the student's and the partner's classwork grade to 100,
but if not, set the student's classwork grade to 85; either way, print a message about what happened.
In main:
- Create a Grade and set its instance variables, then compute the final grade
- Create a Student, and set its instance variables, giving it the existing grade as its grade.
Use the aceTest() and markClasswork() methods, then compute the student's final grade again.
- Create another student, and set their name and create for them a Grade and set its instance vars.
Make the students be each other's partner, have the second student call markClasswork(),
then compute the second student's final grade.