|
1 | 1 | package com.codefortomorrow.advanced.chapter15.solutions;
|
| 2 | + |
2 | 3 | import java.util.Arrays;
|
| 4 | + |
3 | 5 | /*
|
4 | 6 | Create a Measurable interface
|
5 | 7 | that is used to “measure” anything. Add necessary methods.
|
|
12 | 14 |
|
13 | 15 | Problem adapted from Java SE 9 Textbook
|
14 | 16 | */
|
15 |
| -public class MeasurableProblem{ |
16 |
| - public static void main(String[] args){ |
17 |
| - Object[] students = { |
18 |
| - new Student("John", 2.7), |
19 |
| - new Student("Josh", 1.2), |
20 |
| - new Student("Jacob", 3.4), |
21 |
| - new Student("Rebecca", 3.5), |
22 |
| - new Student("Arnav", 4.0) |
23 |
| - }; |
24 |
| - Student test = new Student(); |
25 |
| - System.out.println("Average: " + test.average(students)); |
26 |
| - System.out.println("Largest: " + test.largest(students)); |
27 |
| - System.out.println("Smallest: " + test.smallest(students)); |
28 |
| - } |
| 17 | +public class MeasurableProblem { |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + Object[] students = { |
| 21 | + new Student("John", 2.7), |
| 22 | + new Student("Josh", 1.2), |
| 23 | + new Student("Jacob", 3.4), |
| 24 | + new Student("Rebecca", 3.5), |
| 25 | + new Student("Arnav", 4.0), |
| 26 | + }; |
| 27 | + Student test = new Student(); |
| 28 | + System.out.println("Average: " + test.average(students)); |
| 29 | + System.out.println("Largest: " + test.largest(students)); |
| 30 | + System.out.println("Smallest: " + test.smallest(students)); |
| 31 | + } |
29 | 32 | }
|
30 |
| -interface Measurable{ |
31 |
| - abstract double average(Object[] students); |
32 |
| - abstract String largest(Object[] students); |
33 |
| - abstract String smallest(Object[] students); |
| 33 | + |
| 34 | +interface Measurable { |
| 35 | + abstract double average(Object[] students); |
| 36 | + |
| 37 | + abstract String largest(Object[] students); |
| 38 | + |
| 39 | + abstract String smallest(Object[] students); |
34 | 40 | }
|
35 |
| -class Student implements Measurable{ |
36 |
| - private double GPA; |
37 |
| - private String name; |
38 |
| - public Student(String name, double GPA){ |
39 |
| - this.name = name; |
40 |
| - this.GPA = GPA; |
41 |
| - } |
42 |
| - public Student(){ |
43 |
| - name = ""; |
44 |
| - GPA = 0.0; |
45 |
| - } |
46 |
| - public double average(Object[] orig){ |
47 |
| - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
48 |
| - double sum = 0; |
49 |
| - for(Student s: students) |
50 |
| - sum += s.getGPA(); |
51 |
| - return sum / students.length; |
52 |
| - } |
53 |
| - public String largest(Object[] orig){ |
54 |
| - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
55 |
| - Student curr = students[0]; |
56 |
| - for(Student s: students){ |
57 |
| - if(s.getGPA() > curr.getGPA()) |
58 |
| - curr = s; |
| 41 | + |
| 42 | +class Student implements Measurable { |
| 43 | + private double GPA; |
| 44 | + private String name; |
| 45 | + |
| 46 | + public Student(String name, double GPA) { |
| 47 | + this.name = name; |
| 48 | + this.GPA = GPA; |
| 49 | + } |
| 50 | + |
| 51 | + public Student() { |
| 52 | + name = ""; |
| 53 | + GPA = 0.0; |
| 54 | + } |
| 55 | + |
| 56 | + public double average(Object[] orig) { |
| 57 | + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
| 58 | + double sum = 0; |
| 59 | + for (Student s : students) sum += s.getGPA(); |
| 60 | + return sum / students.length; |
| 61 | + } |
| 62 | + |
| 63 | + public String largest(Object[] orig) { |
| 64 | + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
| 65 | + Student curr = students[0]; |
| 66 | + for (Student s : students) { |
| 67 | + if (s.getGPA() > curr.getGPA()) curr = s; |
| 68 | + } |
| 69 | + return curr.getName(); |
59 | 70 | }
|
60 |
| - return curr.getName(); |
61 |
| - } |
62 |
| - public String smallest(Object[] orig){ |
63 |
| - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
64 |
| - Student curr = students[0]; |
65 |
| - for(Student s: students){ |
66 |
| - if(s.getGPA() < curr.getGPA()) |
67 |
| - curr = s; |
| 71 | + |
| 72 | + public String smallest(Object[] orig) { |
| 73 | + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting |
| 74 | + Student curr = students[0]; |
| 75 | + for (Student s : students) { |
| 76 | + if (s.getGPA() < curr.getGPA()) curr = s; |
| 77 | + } |
| 78 | + return curr.getName(); |
| 79 | + } |
| 80 | + |
| 81 | + public double getGPA() { |
| 82 | + return GPA; |
| 83 | + } |
| 84 | + |
| 85 | + public String getName() { |
| 86 | + return name; |
68 | 87 | }
|
69 |
| - return curr.getName(); |
70 |
| - } |
71 |
| - public double getGPA(){ |
72 |
| - return GPA; |
73 |
| - } |
74 |
| - public String getName(){ |
75 |
| - return name; |
76 |
| - } |
77 | 88 | }
|
0 commit comments