java 简单程序 实例

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Usel Skel_13
Account ------------------------------------name: String -balance:int -----------------------------------+Account(String n, int b) +Account() +void deposit(int amt) +boolean withdraw(int amt) +int read_balance()
}}
16
Ex13 . Create a Test class and an Account class:
(1 attribute balance)
It should have a menu as follows: Banking Application =============== 1. Deposit 2. Withdraw 3. Check Balance
Make it Menu Driven:
20
Exercise 1_7 Extra Question Complete the following application
Match team1: String team2 : String score1 : integer score2 : integer Match(String t1, String t2) void team1_score() void team2_score() void print_score() Make it Menu Driven:
synchronised this throw throws transient true try void volatile while
2
Primitive Types
3
package ch1; public class Counter { private int value; public Counter() {value=0;} public Counter(int v) {value=v1;} public void increment(){ this.value++;} public void decrement(){ this.value--;} public int read_value(){ return this.value; } }
}
12
Ex11
Run
Skel1 Program
Ex12: Add an extra item to Menu: : : 4. Reset Counter to 0 5. Exit
13
package ch1; public class Counter { private int value; public Counter() {value=0;} public Counter(int v) {value=v1;} : public void step_up(int amt){ this.value += amt; } public boolean step_down(int amt){ if (amt > this.value) return false; else { this.value -= amt; return true;} } }
Make it menu Driven: 1. Increment Second 2. Read total in seconds 3. Print (in form 2:45:21)
18
Exercise 1_5 Complete the following application
Team games_played points Use: Skel_15 Team() void win() void draw() void loss() void print_details() Make it Menu Driven: Uses Serialization
:
8
ຫໍສະໝຸດ Baidu
public void draw_horz() { System.out.println(); for(int i=0;i<this.size1;i++) System.out.print(" *"); }
public void draw_vert() { System.out.println(); for(int i=0;i<this.size1;i++) System.out.println(" *"); } }
System.out.println(“8.Exit");
choice = Console.readInt("Please enter a value-->");
15
: else if(choice==6) { int amt=Console.readInt(“Enter Amount:”); c2.step_up(amt); } else if(choice==7) { int amt=Console.readInt(“Enter Amount:”); boolean res = c2.step_down(amt); if (res== false) { System.out.println(“error- too much”);} } } // end while
14
package ch1; public class TestCounter { public static void main(String[] args) { : System.out.println(“6. Step up value:"); System.out.println(“7. Step down value:");
}}
6
7
import java.io.Serializable; public class Shape implements Serializable{ private int size1; private int size2; public Shape(int s1) { this.size1=s1; this.size2=0;} public Shape(int s1,int s2) { this.size1=s1; this.size2=s2;}
17
Exercise 1_4 Complete the following application
Time hour, minute, second Time(int h, int m, int s) void incr_sec() int total_in_sec() void print()
Use: Skel_14
choice = Console.readInt("Please enter a value-->"); 5
if(choice==1) { c2.increment(); } else if(choice==2) { c2.decrement(); } else if(choice==3) { int res=c2.read_value(); System.out.println("Value = "+res); } } // end while
4
package ch1; public class TestCounter { public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(2); int choice = 1; while(choice!=4) { System.out.println(); System.out.println("1.Increment"); System.out.println("2.Decrement"); System.out.println("3.Read Value"); System.out.println("4.Exit");
No Skel Uses Serialization
21
Exercise 1_8
Extra Question
Complete the following application by adding Functionality as outlined: (i)prints out a diagonal line * * * (ii) print a square
Software Design 41
Chapter 1
OO Review - Encapsulation
1
Java Keywords
(see Appendix 1 - Core Java)
abstract boolean break byte case catch char class const continue
(v)
Modify (iv) so it prints the rectangle with a given symbol void draw_empty_rect2(char s) &&&&& & & & & &&&&&
9
public class ShapeTest {
public static void main(String[] args) { Shape s=new Shape(10,13); try{ ObjectInputStream in=new ObjectInputStream( new FileInputStream("test.dat")); s=(Shape)in.readObject(); in.close();} catch(Exception e){}
default do double else extends false final finally float for
goto if implements import instanceof int interface long native new
null package private protected public return short static super switch
Skel_18
Uses Serialization
*
* * * * * * * * * * * * * * * * * * * * * * * * * *
(iii) print a rectangle
***** ***** ***** *****
22
(iv) prints the rectangle in the following format: ***** * * * * *****
19
Exercise 1_6 Complete the following application
Boat_club total_boats boats_hired Use: Skel_16 Boat-Club(int t) boolean loan() boolean bring_back() int read_avail() Uses Serialization
int choice = 1;
10
: while(choice!=3) { System.out.println(); System.out.println("1.Draw Horizontal"); System.out.println("2.Draw Vertical"); System.out.println("3.Exit"); choice = Console.readInt("Please enter a value-->"); if(choice==1){ s.draw_horz();} else if(choice==2){ s.draw_vert();}
} // end while
:
11
:
try{ ObjectOutputStream out=new ObjectOutputStream( new FileOutputStream("test.dat")); out.writeObject(s); out.close();} catch(Exception e){} }
相关文档
最新文档