Skip to content

Commit 039ba35

Browse files
committed
interfaces
1 parent 89d8c7d commit 039ba35

File tree

10 files changed

+554
-0
lines changed

10 files changed

+554
-0
lines changed

innerclasses/Example1.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Outer{
2+
Inner outerMethod(){
3+
return new Inner();
4+
}
5+
class Inner{
6+
Inner(){
7+
System.out.println("inner class");
8+
}
9+
10+
}
11+
12+
public static void main(String[] args){
13+
Outer o = new Outer();
14+
Outer.Inner n = o.outerMethod();
15+
}
16+
}

innerclasses/Exercise2.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Big{
2+
private String name;
3+
public Big(String name){
4+
this.name = name;
5+
}
6+
public String toString(){
7+
return name;
8+
}
9+
}
10+
11+
class Sequence{
12+
private Big[] items;
13+
private int next;
14+
public Sequence(int size){
15+
items = new Big[size];
16+
add();
17+
}
18+
19+
Big b = new Big("nonso");
20+
public void add(){
21+
for(next = 0; next < items.length; next++){
22+
items[next] = b;
23+
}
24+
}
25+
26+
public static void main(String[] args){
27+
Sequence sequence = new Sequence(5);
28+
System.out.println(sequence.b);
29+
}
30+
}

interfaces/Apply.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
3+
class Processor {
4+
public String name() {
5+
return getClass().getSimpleName();
6+
}
7+
8+
Object process(Object input) {
9+
return input;
10+
}
11+
}
12+
13+
class Upcase extends Processor {
14+
String process(Object input) { // Covariant return
15+
return ((String) input).toUpperCase();
16+
}
17+
}
18+
19+
class Downcase extends Processor {
20+
String process(Object input) {
21+
return ((String) input).toLowerCase();
22+
}
23+
}
24+
25+
class Splitter extends Processor {
26+
String process(Object input) {
27+
// The split() argument divides a String into pieces:
28+
return Arrays.toString(((String) input).split(" "));
29+
}
30+
}
31+
32+
public class Apply {
33+
public static void process(Processor p, Object s) {
34+
System.out.println("Using Processor " + p.name());
35+
System.out.println(p.process(s));
36+
}
37+
38+
public static String s = "Disagreement with beliefs is by definition incorrect";
39+
40+
public static void main(String[] args) {
41+
process(new Upcase(), s);
42+
process(new Downcase(), s);
43+
process(new Splitter(), s);
44+
}
45+
}

interfaces/Exercise17.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
interface Days {
2+
int SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4,
3+
THURSDAY = 5, FRIDAY = 6, SATURDAY = 7;
4+
}
5+
6+
class Week implements Days {
7+
private static int count = 0;
8+
private int id = count++;
9+
public Week() { System.out.println("Week() " + id); }
10+
}
11+
12+
public class Exercise17 {
13+
public static void main(String[] args) {
14+
// Without any objects, static fields exist:
15+
System.out.println("MONDAY = " + Days.MONDAY);
16+
Week w1 = new Week();
17+
// Error: cannot assign a value to final variable SUNDAY:
18+
// w1.SUNDAY = 2;
19+
}
20+
}

interfaces/Exercise18.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
interface Cycle{
2+
void ride();
3+
}
4+
5+
interface CycleFactory{
6+
Cycle getCycle();
7+
}
8+
9+
class Unicycle implements Cycle{
10+
@Override
11+
public void ride(){
12+
System.out.println("unicycle ride");
13+
}
14+
15+
public String toString(){
16+
String d = "unicycle";
17+
return d;
18+
}
19+
}
20+
21+
class UnicycleFactory implements CycleFactory{
22+
23+
@Override
24+
public Cycle getCycle() {
25+
return new Unicycle();
26+
}
27+
28+
}
29+
30+
class Bicycle implements Cycle{
31+
@Override
32+
public void ride(){
33+
System.out.println("bicycle ride");
34+
}
35+
36+
public String toString(){
37+
String d = "Bicycle";
38+
return d;
39+
}
40+
}
41+
42+
class BicycleFactory implements CycleFactory{
43+
44+
@Override
45+
public Cycle getCycle() {
46+
return new Bicycle();
47+
}
48+
49+
}
50+
51+
class Tricycle implements Cycle{
52+
@Override
53+
public void ride(){
54+
System.out.println("tricycle ride");
55+
}
56+
57+
public String toString(){
58+
String d = "Tricycle";
59+
return d;
60+
}
61+
}
62+
63+
class TricycleFactory implements CycleFactory{
64+
65+
@Override
66+
public Cycle getCycle() {
67+
return new Tricycle();
68+
}
69+
70+
}
71+
72+
public class Exercise18 {
73+
public static void rideCycle(CycleFactory factory) {
74+
Cycle c = factory.getCycle();
75+
c.ride();
76+
}
77+
public static void main(String [] args) {
78+
rideCycle(new UnicycleFactory());
79+
rideCycle(new BicycleFactory());
80+
rideCycle(new TricycleFactory());
81+
}
82+
}

interfaces/Exercise19.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.*;
2+
3+
interface Games {
4+
void play();
5+
}
6+
7+
interface GamesFactory {
8+
Games getGames();
9+
}
10+
11+
class CoinToss implements Games {
12+
Random rand = new Random();
13+
public void play() {
14+
System.out.println("Toss Coin: ");
15+
switch(rand.nextInt(2)) {
16+
case 0 : System.out.println("Heads"); return;
17+
case 1 : System.out.println("Tails"); return;
18+
default: System.out.println("OnEdge"); return;
19+
}
20+
}
21+
}
22+
23+
class CoinTossFactory implements GamesFactory {
24+
public Games getGames() {
25+
return new CoinToss();
26+
}
27+
}
28+
29+
class DiceThrow implements Games {
30+
Random rand = new Random();
31+
public void play() {
32+
System.out.println("Throw Dice: " + (rand.nextInt(6) + 1));
33+
}
34+
}
35+
36+
class DiceThrowFactory implements GamesFactory {
37+
public Games getGames() {
38+
return new DiceThrow();
39+
}
40+
}
41+
42+
43+
public class Exercise19 {
44+
public static void playGame(GamesFactory factory) {
45+
Games g = factory.getGames();
46+
g.play();
47+
}
48+
public static void main(String [] args) {
49+
playGame(new CoinTossFactory());
50+
playGame(new DiceThrowFactory());
51+
}
52+
}

interfaces/Exercise2.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
abstract class Noinstance{
3+
Noinstance(){
4+
System.out.println("abstr constr");
5+
}
6+
public abstract void meth();
7+
8+
public abstract void eth();
9+
10+
public void try4(){
11+
12+
}
13+
}
14+
15+
interface Test{
16+
int name = 5;
17+
18+
void func();
19+
}
20+
21+
interface Test2 extends Test{
22+
void destroy();
23+
}
24+
25+
26+
27+
class Waveform implements Test{
28+
public static String name = "name";
29+
public String toString(){
30+
String d = "Wave";
31+
return d;
32+
}
33+
34+
@Override
35+
public void func(){
36+
System.out.println("func()");
37+
}
38+
}
39+
40+
class Troy extends Waveform implements Test2{
41+
42+
@Override
43+
public void destroy(){
44+
func();
45+
}
46+
}
47+
48+
public class Exercise2 extends Noinstance{
49+
50+
@Override
51+
public void meth() {
52+
System.out.println("hello meth");
53+
54+
}
55+
56+
@Override
57+
public void eth(){
58+
System.out.println("hello eth");
59+
}
60+
61+
static void upcast(Test g){
62+
g.func();
63+
}
64+
65+
public static void main(String[] args){
66+
Waveform w = new Waveform();
67+
Exercise2 ex = new Exercise2();
68+
upcast(w);
69+
ex.meth();
70+
ex.eth();
71+
System.out.println(w);
72+
}
73+
// Error; cant instantiate class
74+
// Noinstance none = Noinstance();
75+
}

0 commit comments

Comments
 (0)