Last active
July 25, 2016 23:56
-
-
Save Nexuist/562d1691c43d16844164217c6dae81b3 to your computer and use it in GitHub Desktop.
Demonstrating inheritance, scope, and ArrayList usage for CS115
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************************************************** | |
// Student.java | |
// | |
// Represents a college student. | |
//******************************************************************** | |
import java.util.ArrayList; | |
public class Student { | |
private String firstName, lastName; | |
private Address homeAddress, schoolAddress; | |
private ArrayList<Double> testScores; | |
//----------------------------------------------------------------- | |
// Constructor: Sets up this student with the specified values. | |
//----------------------------------------------------------------- | |
public Student(String first, String last, Address home, Address school) { | |
firstName = first; | |
lastName = last; | |
homeAddress = home; | |
schoolAddress = school; | |
testScores = new ArrayList<Double>(3); | |
testScores.add(0d); | |
testScores.add(0d); | |
testScores.add(0d); | |
} | |
public Student(String first, String last, Address home, Address school, double testScore1, double testScore2, double testScore3) { | |
this(first, last, home, school); | |
setTestScore(1, testScore1); | |
setTestScore(2, testScore2); | |
setTestScore(3, testScore3); | |
} | |
public double getTestScore(int testNumber) { | |
try { | |
return testScores.get(testNumber - 1); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return 0; | |
} | |
public void setTestScore(int testNumber, double score) { | |
try { | |
testScores.set(testNumber - 1, score); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public double average() { | |
double sum = 0; | |
for (Double score: testScores) { | |
sum += score; | |
} | |
return sum / testScores.size(); | |
} | |
//----------------------------------------------------------------- | |
// Returns a string description of this Student object. | |
//----------------------------------------------------------------- | |
public String toString() { | |
String result; | |
result = firstName + " " + lastName + "\n"; | |
result += "Home Address:\n" + homeAddress + "\n"; | |
result += "School Address:\n" + schoolAddress + "\n"; | |
result += "Test Scores:\n" + testScores; | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************************************************** | |
// StudentBody.java | |
// | |
// Demonstrates the use of an aggregate class. | |
//******************************************************************** | |
public class StudentBody { | |
//----------------------------------------------------------------- | |
// Creates some Address and Student objects and prints them. | |
//----------------------------------------------------------------- | |
public static void main(String[] args) | |
{ | |
Address school = new Address("800 Lancaster Ave.", "Villanova", | |
"PA", 19085); | |
Address jHome = new Address("21 Jump Street", "Lynchburg", | |
"VA", 24551); | |
Student john = new Student("John", "Smith", jHome, school); | |
Address mHome = new Address("123 Main Street", "Euclid", "OH", | |
44132); | |
Student marsha = new Student("Marsha", "Jones", mHome, school, 12, 80, 100); | |
System.out.println(john); | |
System.out.println(); | |
System.out.println(marsha); | |
System.out.println("Marsha's Average: " + marsha.average()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment