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.
- A game has owls carrying messages between wizards. Each wizard lives in a tower that is sometimes surrounded by wind or rain storms.
Messages may include treasure which could fall out if the message is dropped, or which might be magical and explode when wet.
Different owls have different speeds, strengths, and health. Create an object oriented design for
this scenario, including names for classes, characteristics, and behavior methods. You are only being asked for the design; you do NOT need to write any code. (There
is more than one right answer, your design should show me that you thought through the situation in an object oriented way)
Part B
We will use Java to do a few programming tasks. Here is an online Java IDE -- an editor that also lets you
run code right in the browser. Make a new .java file
More about using the IDE for this course.
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 real Java, different classes should live in different files, but we are just trying to get the basics to work. Your file should be Main.java
as usual and the class Main should have public in front of it. Other classes in the same file do not get the keyword public.
// this file is Main.java
// Dog class, no public keyword
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
// Cat class (not public)
class Cat {
public String name;
public void miaow() {
System.out.println(name + " says miaow");
}
}// end of Cat class
// Main class is public to match the file 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. This class can be above or below the Main class, but not inside it.
-
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 -- f they have a partner (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 first student be the second student's partner, have the second student call markClasswork(),
then compute the second student's final grade.