Explorar el Código

ye olde code 1

Kr Younger hace 8 años
commit
25e22bfe3c
Se han modificado 17 ficheros con 765 adiciones y 0 borrados
  1. 25
    0
      .gitignore
  2. 50
    0
      Account.java
  3. 7
    0
      AccountException.java
  4. 118
    0
      Calendar.java
  5. 16
    0
      Calendar1.java
  6. 45
    0
      CalendarGraphic.java
  7. 46
    0
      Date.java
  8. 7
    0
      DateException.java
  9. 32
    0
      DepositAccount.java
  10. 12
    0
      README.TXT
  11. 36
    0
      README.md
  12. 40
    0
      Scrabble.java
  13. 16
    0
      Squares.java
  14. 141
    0
      TextIO.java
  15. 22
    0
      TriangleOne.java
  16. 30
    0
      TriangleTwo.java
  17. 122
    0
      package.bluej

+ 25
- 0
.gitignore Ver fichero

@@ -0,0 +1,25 @@
1
+# ---> Java
2
+# Compiled class file
3
+*.class
4
+
5
+# Log file
6
+*.log
7
+
8
+# BlueJ files
9
+*.ctxt
10
+
11
+# Mobile Tools for Java (J2ME)
12
+.mtj.tmp/
13
+
14
+# Package Files #
15
+*.jar
16
+*.war
17
+*.ear
18
+*.zip
19
+*.tar.gz
20
+*.rar
21
+
22
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23
+hs_err_pid*
24
+
25
+.DS_Store

+ 50
- 0
Account.java Ver fichero

@@ -0,0 +1,50 @@
1
+
2
+class Account
3
+{
4
+    private int amount;
5
+
6
+    public Account()
7
+    {
8
+        amount = 0;
9
+    }
10
+
11
+    public Account(int sum)
12
+    {
13
+        amount=sum;
14
+    }
15
+
16
+    public void deposit(int sum)
17
+    {
18
+        amount+=sum;
19
+    }
20
+
21
+    public boolean transfer(Account acc, int sum) {
22
+        if(sum<=amount) {
23
+            amount-=sum;
24
+            acc.amount+=sum;
25
+            return true;
26
+        }
27
+        else return false;
28
+    }
29
+
30
+    public Account open(int sum) {
31
+        if(sum<=amount) {
32
+            amount-=sum;
33
+            return new Account(sum);
34
+        }
35
+        else return null;
36
+    }
37
+
38
+    public int balance() {
39
+        return amount;
40
+    }
41
+
42
+    public void withdraw(int sum) throws AccountException
43
+    {
44
+        if(sum<=amount)
45
+            amount-=sum;
46
+        else
47
+            throw new AccountException("overdraw");
48
+    }
49
+
50
+}

+ 7
- 0
AccountException.java Ver fichero

@@ -0,0 +1,7 @@
1
+class AccountException extends Exception
2
+{
3
+ AccountException(String s)
4
+ {
5
+  super(s);
6
+ }
7
+}

+ 118
- 0
Calendar.java Ver fichero

@@ -0,0 +1,118 @@
1
+class Calendar extends Date
2
+{
3
+ 
4
+ public Calendar(int d,int m,int y) throws DateException
5
+ {
6
+  super(d,m,y);
7
+ } 
8
+
9
+ public void addDay()
10
+ {
11
+  day++;
12
+  if(day==32)
13
+    {
14
+     day=1;
15
+     month++;
16
+    }
17
+  else if(day==31&&(month==4||month==6||month==9||month==11))
18
+    {
19
+     day=1;
20
+     month++;
21
+    }
22
+  else if(day==30&&month==2)
23
+    {
24
+     day=1;
25
+     month=3;
26
+    }
27
+  else if(day==29&&month==2&&year%4!=0)
28
+    {
29
+     day=1;
30
+     month=3;
31
+    }
32
+  if(month==13)
33
+    {
34
+     month=1;
35
+     year++;
36
+    }
37
+ }
38
+
39
+ public void subDay()
40
+  {
41
+  day--;
42
+  if(day==0)
43
+    {
44
+     month--;
45
+     if(month==0)
46
+       {
47
+        month=12;
48
+        day=31;
49
+        year--;
50
+       }
51
+     else if(month==4||month==6||month==9||month==11)
52
+       day=30;
53
+     else if(month==2)
54
+        if(year%4==0)
55
+           day=29;
56
+        else
57
+           day=28;
58
+     else
59
+        day=31;
60
+    }
61
+ }
62
+
63
+ public void addMonth()
64
+ {
65
+  month++;
66
+  if(month==13)
67
+     {
68
+      month=1;
69
+      year++;
70
+     }
71
+  else if(day==31&&(month==4||month==6||month==9||month==11))
72
+     day=30;
73
+  else if(month==2)
74
+     if(year%4==0)
75
+       {
76
+        if(day>29)
77
+           day=29;
78
+       }
79
+     else if(day>28)
80
+        day=28;
81
+ }
82
+
83
+ public void subMonth()
84
+ {
85
+  month--;
86
+  if(month==0)
87
+     {
88
+      month=12;
89
+      year--;
90
+     }
91
+  else if(day==31&&(month==4||month==6||month==9||month==11))
92
+     day=30;
93
+  else if(month==2)
94
+     if(year%4==0)
95
+       {
96
+        if(day>29)
97
+           day=29;
98
+       }
99
+     else if(day>28)
100
+        day=28;
101
+ }
102
+
103
+ public void addYear()
104
+ {
105
+  year++;
106
+  if(month==2&&day==29)
107
+     day=28;
108
+ }
109
+
110
+ public void subYear()
111
+ {
112
+  year--;
113
+  if(month==2&&day==29)
114
+     day=28;
115
+ }
116
+
117
+}
118
+

+ 16
- 0
Calendar1.java Ver fichero

@@ -0,0 +1,16 @@
1
+class Calendar1 extends Calendar
2
+{
3
+CalendarGraphic picture;
4
+ 
5
+ public Calendar1(int d,int m,int y) throws DateException
6
+ {
7
+  super(d,m,y);
8
+  picture = new CalendarGraphic(d,m,y);
9
+ } 
10
+
11
+ public void display()
12
+ {
13
+  picture.display(day,month,year);
14
+ }
15
+
16
+}

+ 45
- 0
CalendarGraphic.java Ver fichero

@@ -0,0 +1,45 @@
1
+import java.awt.*;
2
+
3
+class CalendarGraphic extends Frame
4
+{
5
+TextField daytext,monthtext,yeartext;
6
+
7
+   public CalendarGraphic(int d,int m,int y)
8
+   {
9
+    setTitle("Date display");
10
+    setSize(300,100);
11
+    setLayout(new FlowLayout());
12
+    daytext = new TextField(2);
13
+    monthtext = new TextField(3);
14
+    yeartext = new TextField(4);
15
+    add(daytext);
16
+    add(monthtext);
17
+    add(yeartext);
18
+    setVisible(true);
19
+    display(d,m,y);
20
+   }
21
+
22
+   public void display(int day,int month,int year)
23
+   {
24
+    String monthname="";
25
+    switch(month)
26
+       {
27
+        case 1: monthname="JAN"; break;
28
+        case 2: monthname="FEB"; break;
29
+        case 3: monthname="MAR"; break;
30
+        case 4: monthname="APR"; break;
31
+        case 5: monthname="MAY"; break;
32
+        case 6: monthname="JUN"; break;
33
+        case 7: monthname="JUL"; break;
34
+        case 8: monthname="AUG"; break;
35
+        case 9: monthname="SEP"; break;
36
+        case 10: monthname="OCT"; break;
37
+        case 11: monthname="NOV"; break;
38
+        case 12: monthname="DEC"; break;
39
+       }
40
+    daytext.setText(Integer.toString(day));
41
+    monthtext.setText(monthname);
42
+    yeartext.setText(Integer.toString(year));
43
+   }
44
+ 
45
+}

+ 46
- 0
Date.java Ver fichero

@@ -0,0 +1,46 @@
1
+class Date
2
+{
3
+ protected int day,month,year;
4
+
5
+ public Date(int d,int m,int y) throws DateException
6
+ {
7
+  day=d;
8
+  month=m;
9
+  year=y;
10
+  if(month<1||month>12||day<1)
11
+     throw new DateException(toString());
12
+  else if((month==4||month==6||month==9||month==11)&&day>30)
13
+     throw new DateException(toString());
14
+  else if(month==2)
15
+     {
16
+      if(year%4==0)
17
+         {
18
+          if(day>29)
19
+             throw new DateException(toString());
20
+         }
21
+      else if(day>28)
22
+         throw new DateException(toString());
23
+     }
24
+  else if(day>31)
25
+      throw new DateException(toString());
26
+ }
27
+
28
+ public String toString()
29
+ {
30
+  return day+"/"+month+"/"+year;
31
+ }
32
+
33
+ public boolean lessThan(Date d)
34
+ {
35
+  if(year<d.year)
36
+     return true;
37
+  else if(year==d.year)
38
+     if(month<d.month)
39
+        return true;
40
+     else if(month==d.month)
41
+        if(day<d.day)
42
+           return true;
43
+  return false;
44
+ }
45
+
46
+}

+ 7
- 0
DateException.java Ver fichero

@@ -0,0 +1,7 @@
1
+class DateException extends Exception
2
+{
3
+ DateException(String s)
4
+ {
5
+  super(s);
6
+ }
7
+}

+ 32
- 0
DepositAccount.java Ver fichero

@@ -0,0 +1,32 @@
1
+class DepositAccount extends Account
2
+{
3
+ private boolean newmonth=true;
4
+ private double interest;
5
+
6
+ public DepositAccount(int sum,double interest)
7
+ {
8
+  super(sum);
9
+  this.interest=interest;
10
+ }
11
+
12
+ // 
13
+ public void withdraw(int sum) throws AccountException
14
+    {
15
+        if(sum<=super.balance())
16
+            super.withdraw(sum);
17
+        else
18
+            throw new AccountException("overdraw");
19
+    }
20
+
21
+
22
+ public void update()
23
+ {
24
+  deposit((int)(balance()*interest));
25
+  newmonth=true;
26
+ }
27
+
28
+ public void setInterest(double newInterest)
29
+ {
30
+  interest=newInterest;
31
+ }
32
+}

+ 12
- 0
README.TXT Ver fichero

@@ -0,0 +1,12 @@
1
+------------------------------------------------------------------------
2
+This is the project README file. Here, you should describe your project.
3
+Tell the reader (someone who does not know anything about this project)
4
+all he/she needs to know. The comments should usually include at least:
5
+------------------------------------------------------------------------
6
+
7
+PROJECT TITLE:
8
+PURPOSE OF PROJECT:
9
+VERSION or DATE:
10
+HOW TO START THIS PROJECT:
11
+AUTHORS:
12
+USER INSTRUCTIONS:

+ 36
- 0
README.md Ver fichero

@@ -0,0 +1,36 @@
1
+# Some Code Fom Long Ago.
2
+
3
+Java has been around a long time, and this code proves that point.
4
+
5
+Take a look at each class, see how they are linked. They are not all linked together, but each class has something
6
+useful for you find.
7
+
8
+### Scrabble
9
+
10
+Take a look at how a case statement is used instead of a whole lot of
11
+IF statements. Kind of cool, huh?
12
+
13
+### Loops
14
+
15
+#### Squares, TriangleOne, & TriangleTwo
16
+
17
+Dig around and look at how loops are used, even a nested loop or three.
18
+Play around with different parametera to the functions. Run a few.
19
+
20
+### Calendar
21
+
22
+Look at how the logic allows for various calendar operations. Much of this
23
+you can rely on the Java standard library to use, but the ideas here are good to
24
+look at and try to follow. 
25
+
26
+Run CalendarGraphic, and step through the code used to create the simple window. It uses Java's older AWT library, but does show a little bit of how to display in a window.
27
+
28
+### Account
29
+
30
+Notice how we create an Account object and subclass it in DepositAccount. 
31
+What does the exception do? why does this seem like a good idea?
32
+
33
+### TextIO
34
+
35
+Read through this class, carefully. See how the developer is trying to simplify Java
36
+IO? Do what you find there.

+ 40
- 0
Scrabble.java Ver fichero

@@ -0,0 +1,40 @@
1
+import java.io.*;
2
+
3
+class Scrabble
4
+{
5
+// A class to give you the scrabble score of a word
6
+
7
+ public static void main(String[] args) throws IOException
8
+ {
9
+  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
10
+  System.out.print("Enter a word: ");
11
+  String word=in.readLine();
12
+  System.out.println("Scrabble score is: "+score(word));
13
+ }
14
+
15
+ public static int score(String word)
16
+ {
17
+  String uword=word.toUpperCase();
18
+  int score=0;
19
+  for(int i=0;i<uword.length();i++)
20
+     switch(uword.charAt(i))
21
+       {
22
+        case 'Q': case 'Z':
23
+          score+=10; break;
24
+        case 'J': case 'X':
25
+          score+=8; break;
26
+        case 'K':
27
+          score+=5; break;
28
+        case 'F': case 'H': case 'V': case 'W': case 'Y':
29
+          score+=4; break;
30
+        case 'B': case 'C': case 'M': case 'P':
31
+          score+=3; break;
32
+        case 'D': case 'G':
33
+          score+=2;break;
34
+        default:
35
+          score+=1;break;
36
+      } 
37
+  return score;
38
+ }
39
+
40
+}

+ 16
- 0
Squares.java Ver fichero

@@ -0,0 +1,16 @@
1
+class Squares
2
+{
3
+// Print all square numbers up to a fixed limit
4
+
5
+ static final int LIMIT=150;
6
+
7
+ public void display(int limit)
8
+ {
9
+  int i;
10
+  for(i=1;i*i<limit;i++)
11
+     {
12
+      System.out.print("The square of "+i);
13
+      System.out.println(" is "+i*i);
14
+     }
15
+ }
16
+}

+ 141
- 0
TextIO.java Ver fichero

@@ -0,0 +1,141 @@
1
+import java.io.*;
2
+import java.util.*;
3
+import java.text.*;
4
+
5
+public class TextIO {
6
+
7
+  public TextIO () {};
8
+
9
+  /* The All New Famous Text class     by J M Bishop  Aug 1996
10
+   *            revised for Java 1.1 by Alwyn Moolman Aug 1997
11
+   *            revised for efficiency by J M Bishop Dec 1997
12
+   * 
13
+   * Provides simple input from the keyboard and files.
14
+   * Now also has simple output formatting methods
15
+   * and file opening facilities.
16
+   *
17
+   * public static void   prompt (String s)
18
+   * public static int    readInt (BufferedReader in)
19
+   * public static double readDouble (BufferedReader in)  
20
+   * public static String readString (BufferedReader in)  
21
+   * public static char   readChar (BufferedReader in) 
22
+   * public static String writeInt (int number, int align)
23
+   * public static String writeDouble 
24
+                   (double number, int align, int frac)
25
+   * public static BufferedReader open (InputStream in)
26
+   * public static BufferedReader open (String filename)
27
+   * public static PrintWriter create (String filename)
28
+   */
29
+
30
+  private static StringTokenizer T;
31
+  private static String S;
32
+
33
+  public static BufferedReader open (InputStream in)  {
34
+    return new BufferedReader(new InputStreamReader(in));
35
+  }
36
+
37
+  public static BufferedReader open (String filename) 
38
+	throws FileNotFoundException {
39
+    return new BufferedReader (new FileReader (filename));
40
+  }
41
+  
42
+  public static PrintWriter create 
43
+	 (String filename) throws IOException {
44
+    return new PrintWriter (new FileWriter (filename));
45
+  }
46
+
47
+  public static void prompt (String s) {
48
+    System.out.print(s + " ");
49
+    System.out.flush();
50
+  }
51
+
52
+  public static int readInt (BufferedReader in) throws IOException {
53
+      if (T==null) refresh(in);
54
+      while (true) {
55
+        try {
56
+          return Integer.parseInt(T.nextToken());
57
+        }
58
+        catch (NoSuchElementException e1) {
59
+          refresh (in);
60
+        }
61
+        catch (NumberFormatException e2) {
62
+          System.out.println("Error in number, try again.");
63
+        }
64
+      }
65
+   }
66
+
67
+ public static char readChar (BufferedReader in) throws IOException {
68
+      if (T==null) refresh(in);
69
+      while (true) {
70
+        try {
71
+          return T.nextToken().trim().charAt(0);
72
+        }
73
+        catch (NoSuchElementException e1) {
74
+          refresh (in);
75
+        }
76
+      }
77
+   }
78
+
79
+ public static double readDouble (BufferedReader in) throws IOException {
80
+      if (T==null) refresh(in);
81
+      while (true) {
82
+        try {
83
+          String item = T.nextToken();
84
+          return Double.valueOf(item.trim()).doubleValue();
85
+        }
86
+        catch (NoSuchElementException e1) {
87
+          refresh (in);
88
+        }
89
+        catch (NumberFormatException e2) {
90
+          System.out.println("Error in number, try again.");
91
+        }
92
+      }
93
+   }
94
+
95
+  public static String readString (BufferedReader in) throws IOException {
96
+    if (T==null) refresh (in);
97
+    while (true) {
98
+      try {
99
+        return T.nextToken();
100
+      }
101
+      catch (NoSuchElementException e1) {
102
+        refresh (in);
103
+      }
104
+    }
105
+  }
106
+
107
+  // Keep reading.
108
+  private static void refresh (BufferedReader in) throws IOException {
109
+    S = in.readLine ();
110
+    if (S==null) throw new EOFException();
111
+    T = new StringTokenizer (S);
112
+  }
113
+
114
+  //  Write methods
115
+  //  -------------
116
+
117
+  private static DecimalFormat N = new DecimalFormat();
118
+  private static final String spaces = "                    ";
119
+
120
+  public static String writeDouble (double number, int align, int frac) {
121
+    N.setGroupingUsed(false);
122
+    N.setMaximumFractionDigits(frac);
123
+    N.setMinimumFractionDigits(frac);
124
+    String num = N.format(number);
125
+    if (num.length() < align)
126
+      num = spaces.substring(0,align-num.length()) + num;
127
+    return num;
128
+  }
129
+
130
+  // Okay to finish this lab, create a class that uses a few of the methods
131
+  // here. Make sure it runs. Commit the changes and do a pull request.
132
+  public static String writeInt (int number, int align) {
133
+    N.setGroupingUsed(false);
134
+    N.setMaximumFractionDigits(0);
135
+    String num = N.format(number);
136
+    if (num.length() < align)
137
+      num = spaces.substring(0,align-num.length()) + num;
138
+    return num;
139
+
140
+  }
141
+}

+ 22
- 0
TriangleOne.java Ver fichero

@@ -0,0 +1,22 @@
1
+class TriangleOne
2
+{
3
+    //   Print a bottom-left right-angled triangle of stars, of size SIZE
4
+
5
+    static final int SIZE=5;
6
+
7
+    public void display() 
8
+    {
9
+        this.display(SIZE);
10
+    }
11
+
12
+    public void display(int size)
13
+    {
14
+        for(int i=0;i<size;i++)
15
+        {
16
+            for(int j=0;j<=i;j++)
17
+                System.out.print("*");
18
+            System.out.println();
19
+        }
20
+    }
21
+
22
+}

+ 30
- 0
TriangleTwo.java Ver fichero

@@ -0,0 +1,30 @@
1
+class TriangleTwo {
2
+
3
+    //   Print a horizontal row of XTRIANGLES bottom-left right-angled triangles
4
+    //   of stars, each of size SIZE
5
+
6
+    static final int SIZE=6;
7
+    static final int XTRIANGLES=9;
8
+
9
+    public void display() 
10
+    {
11
+        this.display(XTRIANGLES, SIZE);
12
+    }
13
+
14
+    public void display(int num, int size)
15
+    {
16
+        for(int i=0;i<size;i++)
17
+        {
18
+            for(int k=0;k<num;k++)
19
+            {
20
+                int j;
21
+                for(j=0;j<=i;j++)
22
+                    System.out.print("*");
23
+                for(;j<size;j++)
24
+                    System.out.print(" ");
25
+            }
26
+            System.out.println();
27
+        }
28
+    }
29
+
30
+}

+ 122
- 0
package.bluej Ver fichero

@@ -0,0 +1,122 @@
1
+#BlueJ package file
2
+dependency1.from=Account
3
+dependency1.to=AccountException
4
+dependency1.type=UsesDependency
5
+dependency2.from=DepositAccount
6
+dependency2.to=AccountException
7
+dependency2.type=UsesDependency
8
+editor.fx.0.height=722
9
+editor.fx.0.width=800
10
+editor.fx.0.x=114
11
+editor.fx.0.y=175
12
+objectbench.height=101
13
+objectbench.width=461
14
+package.divider.horizontal=0.6
15
+package.divider.vertical=0.8007380073800738
16
+package.editor.height=427
17
+package.editor.width=674
18
+package.editor.x=1006
19
+package.editor.y=133
20
+package.frame.height=600
21
+package.frame.width=800
22
+package.numDependencies=2
23
+package.numTargets=13
24
+package.showExtends=true
25
+package.showUses=true
26
+project.charset=UTF-8
27
+readme.height=58
28
+readme.name=@README
29
+readme.width=47
30
+readme.x=10
31
+readme.y=10
32
+target1.height=50
33
+target1.name=CalendarGraphic
34
+target1.showInterface=false
35
+target1.type=ClassTarget
36
+target1.width=130
37
+target1.x=450
38
+target1.y=190
39
+target10.height=50
40
+target10.name=Scrabble
41
+target10.showInterface=false
42
+target10.type=ClassTarget
43
+target10.width=80
44
+target10.x=10
45
+target10.y=250
46
+target11.height=50
47
+target11.name=TriangleTwo
48
+target11.showInterface=false
49
+target11.type=ClassTarget
50
+target11.width=100
51
+target11.x=120
52
+target11.y=310
53
+target12.height=50
54
+target12.name=AccountException
55
+target12.showInterface=false
56
+target12.type=ClassTarget
57
+target12.width=140
58
+target12.x=190
59
+target12.y=30
60
+target13.height=50
61
+target13.name=DateException
62
+target13.showInterface=false
63
+target13.type=ClassTarget
64
+target13.width=120
65
+target13.x=470
66
+target13.y=340
67
+target2.height=50
68
+target2.name=Account
69
+target2.showInterface=false
70
+target2.type=ClassTarget
71
+target2.width=80
72
+target2.x=50
73
+target2.y=110
74
+target3.height=50
75
+target3.name=Calendar1
76
+target3.showInterface=false
77
+target3.type=ClassTarget
78
+target3.width=90
79
+target3.x=330
80
+target3.y=220
81
+target4.height=50
82
+target4.name=Squares
83
+target4.showInterface=false
84
+target4.type=ClassTarget
85
+target4.width=80
86
+target4.x=100
87
+target4.y=250
88
+target5.height=50
89
+target5.name=Calendar
90
+target5.showInterface=false
91
+target5.type=ClassTarget
92
+target5.width=80
93
+target5.x=490
94
+target5.y=270
95
+target6.height=50
96
+target6.name=TriangleOne
97
+target6.showInterface=false
98
+target6.type=ClassTarget
99
+target6.width=100
100
+target6.x=10
101
+target6.y=310
102
+target7.height=50
103
+target7.name=TextIO
104
+target7.showInterface=false
105
+target7.type=ClassTarget
106
+target7.width=80
107
+target7.x=490
108
+target7.y=40
109
+target8.height=50
110
+target8.name=Date
111
+target8.showInterface=false
112
+target8.type=ClassTarget
113
+target8.width=80
114
+target8.x=350
115
+target8.y=290
116
+target9.height=50
117
+target9.name=DepositAccount
118
+target9.showInterface=false
119
+target9.type=ClassTarget
120
+target9.width=120
121
+target9.x=170
122
+target9.y=120