Todays topics Java Numbers Iteration Upcoming More Java
Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3 Comp. Sci 001 7. 1
Numbers v v Have talked about and used numbers before Have built-in or “primitive types” q q v int for whole numbers double for numbers that may include fractions One of simplest uses for integers is counting q q The following example counts number of times button is pressed Includes statement: num. Total = num. Total + 1; o Not an equality! o Evaluate right; copy into left Comp. Sci 001 7. 2
Counting Example public class Count extends java. applet. Applet implements Action. Listener { Text. Field m. Query, m. Total; Button b. Count; int no. Total = 0; public void init (){ m. Query = new Text. Field(80); m. Query. set. Text( "Keep track of attendance by pushing the button. "); b. Count = new Button("Register"); m. Total = new Text. Field(40); b. Count. add. Action. Listener(this); add(m. Query); add(b. Count); add(m. Total); m. Total. set. Text("The total attendance is “ + no. Total); } Comp. Sci 001 7. 3
Counting Example (P 2) public void action. Performed(Action. Event event) { Object cause = event. get. Source(); if (cause == b. Count) // (could have omitted) { no. Total = no. Total + 1; m. Total. set. Text("The total attendance is " + no. Total); } } } v Can extend this to multiple counts Comp. Sci 001 7. 4
Tallies public class Tallies extends java. applet. Applet implements Action. Listener { Text. Field m. Query, m. Ans. Stu, m. Ans. Fac, m. Ans. Sta, m. Total; Button b. Students, b. Faculty, b. Staff; int no. Stu=0, no. Fac=0, no. Sta=0, no. Total=0; public void init (){ m. Query = new Text. Field(80); m. Query. set. Text( "Keep track of attendance by pushing the buttons. "); b. Students = new Button("Students"); b. Faculty = new Button("Faculty"); b. Staff = new Button("Staff"); m. Ans. Stu = new Text. Field(12); m. Ans. Fac = new Text. Field(12); Comp. Sci 001 7. 5
Tallies (p 2) m. Ans. Sta = new Text. Field(12); m. Total = new Text. Field(80); b. Students. add. Action. Listener(this); b. Faculty. add. Action. Listener(this); b. Staff. add. Action. Listener(this); add(m. Query); add(b. Students); add(b. Faculty); add(b. Staff); add(m. Total); add(m. Ans. Stu); add(m. Ans. Fac); add(m. Ans. Sta); m. Total. set. Text("The total attendance is " + no. Total + " Subtotals are shown below. "); m. Ans. Stu. set. Text(no. Stu + " students"); m. Ans. Fac. set. Text(no. Fac + " faculty"); m. Ans. Sta. set. Text(no. Sta + " staff"); } Comp. Sci 001 7. 6
Tallies (p 3) public void action. Performed(Action. Event event) { Object cause = event. get. Source(); if (cause == b. Students){ no. Stu = no. Stu + 1; no. Total = no. Total + 1; m. Total. set. Text("The total attendance is " + no. Total + " Subtotals are shown below. "); m. Ans. Stu. set. Text(no. Stu + " students"); } // similar blocks follow for faculty & staff Comp. Sci 001 7. 7
Tallies (p 4) if (cause == b. Faculty){ no. Fac = no. Fac + 1; no. Total = no. Total + 1; m. Total. set. Text("The total attendance is " + " Subtotals are shown m. Ans. Fac. set. Text(no. Fac + " faculty"); } if (cause == b. Staff){ no. Sta = no. Sta + 1; no. Total = no. Total + 1; m. Total. set. Text("The total attendance is " + " Subtotals are shown m. Ans. Sta. set. Text(no. Sta + " staff"); } no. Total + below. "); } } Comp. Sci 001 7. 8
Numbers v Have classes similar to Text. Field to do I/O with integers q q Have used Int. Field methods before Here is class declaration Public class Int. Field { public Int. Field( ); public Int. Field(int size); public void set. Int(int number); public void set. Int(); public int get. Int(); } Comp. Sci 001 7. 9
Doubles Similarly for doubles, we have public class Double. Field { public Double. Field( ); public Double. Field(int size); public void set. Double(double num); public double get. Double( ); } v Have already discussed expressions using ints and doubles v Comp. Sci 001 7. 10
Calculations v v Can use doubles and Double. Fields for real computations Here is an applet to calculate volume of a cylinder q q Need to supply o radius o length Use formula o volume = (area-of-end) * length = Pi * radius * length Comp. Sci 001 7. 11
Calculate volume cylinder public class Numbers extends java. applet. Applet implements Action. Listener { Text. Field instruct, result, m. Radius, m. Length; Double. Field g. Radius, g. Length; Button b. Compute; double radius, length, cyl. Vol, PI=3. 14159265; public void init (){ instruct = new Text. Field(72); instruct. set. Text("Please enter radius and length below. "); m. Radius = new Text. Field(9); m. Radius. set. Text("radius: "); m. Length = new Text. Field(9); m. Length. set. Text("length: "); Comp. Sci 001 7. 12
Calculate volume cylinder (p 2) g. Radius = new Double. Field(10); g. Length = new Double. Field(10); result = new Text. Field(72); result. set. Text( "The volume of the cylinder is: " + cyl. Vol); b. Compute = new Button("Compute"); b. Compute. add. Action. Listener(this); add(instruct); add(m. Radius); add(g. Radius); add(m. Length); add(g. Length); add(b. Compute); add(result); } Comp. Sci 001 7. 13
Calculate volume cylinder (p 3) public void action. Performed(Action. Event event){ Object cause = event. get. Source(); if (cause == b. Compute){ length = g. Length. get. Double(); radius = g. Radius. get. Double(); cyl. Vol = PI * radius * length; result. set. Text( "The volume of the cylinder is: " + cyl. Vol); } } } Comp. Sci 001 7. 14
Iteration v v Iteration -- also called repetition or looping Iteration by Button pushing q q v Often need to repeat a calculation with minor changes Sometimes refine previous solution Sometimes calculate successive values in a series Can do this under control of a Button Applets have the concept of iteration built into their very nature q Waiting for a Button to be pressed implies a loop Comp. Sci 001 7. 15
Iteration by Button pushing example public class But. Compound extends java. applet. Applet implements Action. Listener { Text. Field m. Instruct, m. Balance; Double. Field g. Rate, g. Princ, g. Pay; Button b. Start, b. Next. Installment; double rate, princ, pay, balance; int months; public void init(){ m. Instruct = new Text. Field(80); m. Instruct. set. Text( "Enter principal, rate, payment, then press Start"); Comp. Sci 001 7. 16
Iteration by Button pushing example. 2 g. Princ = new Double. Field(10); g. Rate = new Double. Field(10); g. Pay = new Double. Field(10); m. Balance = new Text. Field(80); b. Start = new Button("Start"); b. Next. Installment = new Button("Next Installment"); b. Start. add. Action. Listener(this); b. Next. Installment. add. Action. Listener(this); add(m. Instruct); add(g. Princ); add(g. Rate); add(g. Pay); add(b. Start); add(b. Next. Installment); add(m. Balance); } Comp. Sci 001 7. 17
Iteration by Button pushing example. 3 public void action. Performed(Action. Event event) { Object cause = event. get. Source(); if (cause == b. Start){ princ = g. Princ. get. Double(); rate = g. Rate. get. Double()/12; pay = g. Pay. get. Double(); months = 0; balance = princ; m. Instruct. set. Text( "Press Next Installment for next Balance"); m. Balance. set. Text( "Start with a balance of " + balance); } Comp. Sci 001 7. 18
Iteration by Button pushing example. 4 if (cause == b. Next. Installment) { months = months + 1; balance = balance*(1. 0 + rate) - pay; m. Balance. set. Text("After " + months + " months at " + 100*rate*12 + "% and payments of " + pay + " the balance is " + balance); } } } Comp. Sci 001 7. 19
- Slides: 19