Skip to content

Commit 4cea04f

Browse files
authored
Merge pull request #16 from code4tomorrow/JoshJ-Problems
Ch. 14 and 15 Problems
2 parents 63bb7f6 + 47f695e commit 4cea04f

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
3+
import java.io.*;
4+
5+
/*
6+
Practice: Use File I/O to compare two files lexicographically.
7+
Lexicographical order is very similar to alphabetical order,
8+
except that it includes more than just lowercase alphabets.
9+
Given two text files f1 and f2, write a program that will
10+
compare their contents and output to the console the
11+
title of the file which comes first lexicographically.
12+
13+
Hint: Java’s String.compareTo method may be of use to you.
14+
*/
15+
16+
public class Lexico {
17+
18+
public static void main(String[] args) {
19+
// Add code here
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And then the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And than the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions;
2+
3+
import java.io.*;
4+
5+
/*
6+
Practice: Use File I/O to compare two files lexicographically.
7+
Lexicographical order is very similar to alphabetical order,
8+
except that it includes more than just lowercase alphabets.
9+
Given two text files f1 and f2, write a program that will
10+
compare their contents and output to the console the
11+
title of the file which comes first lexicographically.
12+
13+
Hint: Java’s String.compareTo method may be of use to you.
14+
*/
15+
16+
public class Lexico {
17+
18+
public static void main(String[] args) {
19+
try (
20+
BufferedReader br = new BufferedReader(new FileReader("f1.txt"));
21+
BufferedReader br2 = new BufferedReader(new FileReader("f2.txt"));
22+
) {
23+
String line1 = br.readLine();
24+
String line2 = br2.readLine();
25+
int c = line1.compareTo(line2);
26+
while (c == 0) {
27+
line1 = br.readLine();
28+
line2 = br2.readLine();
29+
c = line1.compareTo(line2);
30+
}
31+
if (c > 0) System.out.println("File 2"); else System.out.println(
32+
"File 1"
33+
);
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And then the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
All the world's a stage,
2+
And all the men and women merely players;
3+
They have their exits and their entrances,
4+
And one man in his time plays many parts,
5+
His acts being seven ages. At first, the infant,
6+
Mewling and puking in the nurse's arms.
7+
Then the whining schoolboy, with his satchel
8+
And shining morning face, creeping like snail
9+
Unwillingly to school. And then the lover,
10+
Sighing like furnace, with a woeful ballad
11+
Made to his mistress' eyebrow. Then a soldier,
12+
Full of strange oaths and bearded like the pard,
13+
Jealous in honor, sudden and quick in quarrel,
14+
Seeking the bubble reputation
15+
Even in the cannon's mouth. And than the justice,
16+
In fair round belly with good capon lined,
17+
With eyes severe and beard of formal cut,
18+
Full of wise saws and modern instances;
19+
And so he plays his part. The sixth age shifts
20+
Into the lean and slippered pantaloon,
21+
With spectacles on nose and pouch on side;
22+
His youthful hose, well saved, a world too wide
23+
For his shrunk shank, and his big manly voice,
24+
Turning again toward childish treble, pipes
25+
And whistles in his sound. Last scene of all,
26+
That ends this strange eventful history,
27+
Is second childishness and mere oblivion,
28+
Sans teeth, sans eyes, sans taste, sans everything.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codefortomorrow.advanced.chapter15.practice;
2+
3+
/*
4+
Create a Measurable interface
5+
that is used to “measure” anything. Add necessary methods.
6+
Required Methods: double average(Measurable[] items),
7+
largest(Measurable[] items) or smallest(Measurable[] items).
8+
Create a Student class that implements Measurable and
9+
it’s methods(you can compare whatever you measure, i.e. GPA).
10+
Note: for the largest/smallest methods don’t worry
11+
about the case if they are equal.
12+
13+
Problem adapted from Java SE 9 Textbook
14+
*/
15+
public class MeasurableProblem {
16+
17+
public static void main(String[] args) {
18+
//Add testing code
19+
}
20+
}
21+
22+
interface Measurable {
23+
//add interface code here
24+
}
25+
26+
class Student implements Measurable {
27+
//Add student code here
28+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.codefortomorrow.advanced.chapter15.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
Create a Measurable interface
7+
that is used to “measure” anything. Add necessary methods.
8+
Required Methods: double average(Measurable[] items),
9+
largest(Measurable[] items) or smallest(Measurable[] items).
10+
Create a Student class that implements Measurable and
11+
it’s methods(you can compare whatever you measure, i.e. GPA).
12+
Note: for the largest/smallest methods don’t worry
13+
about the case if they are equal.
14+
15+
Problem adapted from Java SE 9 Textbook
16+
*/
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+
}
32+
}
33+
34+
interface Measurable {
35+
abstract double average(Object[] students);
36+
37+
abstract String largest(Object[] students);
38+
39+
abstract String smallest(Object[] students);
40+
}
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();
70+
}
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;
87+
}
88+
}

0 commit comments

Comments
 (0)