Skip to content

Commit d57dbb2

Browse files
committed
Merge branch 'JoshJ-Problems' of https://github.com/code4tomorrow/java into JoshJ-Problems
2 parents 2d5ec4d + 7e8c2fe commit d57dbb2

File tree

4 files changed

+108
-75
lines changed

4 files changed

+108
-75
lines changed

src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
Hint: Java’s String.compareTo method may be of use to you.
1414
*/
1515

16-
public class Lexico{
16+
public class Lexico {
1717

18-
public static void main(String[] args){
18+
public static void main(String[] args) {
1919
// Add code here
2020
}
2121
}

src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
Hint: Java’s String.compareTo method may be of use to you.
1414
*/
1515

16-
public class Lexico{
16+
public class Lexico {
1717

18-
public static void main(String[] args){
19-
BufferedReader br = null;
20-
BufferedReader br2 = null;
21-
try{
22-
br = new BufferedReader(new FileReader("f1.txt"));
23-
br2 = new BufferedReader(new FileReader("f2.txt"));
18+
public static void main(String[] args) {
19+
BufferedReader br = null;
20+
BufferedReader br2 = null;
21+
try {
22+
br = new BufferedReader(new FileReader("f1.txt"));
23+
br2 = new BufferedReader(new FileReader("f2.txt"));
2424

25+
<<<<<<< HEAD
2526
String line1 = br.readLine();
2627
String line2 = br2.readLine();
2728
int c = line1.compareTo(line2);
@@ -41,4 +42,21 @@ public static void main(String[] args){
4142
br2.close();
4243
}
4344
}
45+
=======
46+
String line1 = br.readLine();
47+
String line2 = br2.readLine();
48+
int c = line1.compareTo(line2);
49+
while (c == 0) {
50+
line1 = br.readLine();
51+
line2 = br2.readLine();
52+
c = line1.compareTo(line2);
53+
}
54+
if (c > 0) System.out.println("File 2"); else System.out.println(
55+
"File 1"
56+
);
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
>>>>>>> 7e8c2fe0ebe2b7a31da0266f78e9388b466656c3
4462
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.codefortomorrow.advanced.chapter15.practice;
2+
23
/*
34
Create a Measurable interface
45
that is used to “measure” anything. Add necessary methods.
@@ -11,14 +12,17 @@
1112
1213
Problem adapted from Java SE 9 Textbook
1314
*/
14-
public class MeasurableProblem{
15-
public static void main(String[] args){
16-
//Add testing code
17-
}
15+
public class MeasurableProblem {
16+
17+
public static void main(String[] args) {
18+
//Add testing code
19+
}
1820
}
19-
interface Measurable{
20-
//add interface code here
21+
22+
interface Measurable {
23+
//add interface code here
2124
}
22-
class Student implements Measurable{
23-
//Add student code here
25+
26+
class Student implements Measurable {
27+
//Add student code here
2428
}
Lines changed: 69 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.codefortomorrow.advanced.chapter15.solutions;
2+
23
import java.util.Arrays;
4+
35
/*
46
Create a Measurable interface
57
that is used to “measure” anything. Add necessary methods.
@@ -12,66 +14,75 @@
1214
1315
Problem adapted from Java SE 9 Textbook
1416
*/
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+
}
2932
}
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);
3440
}
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();
5970
}
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;
6887
}
69-
return curr.getName();
70-
}
71-
public double getGPA(){
72-
return GPA;
73-
}
74-
public String getName(){
75-
return name;
76-
}
7788
}

0 commit comments

Comments
 (0)