JavaOOP_Lab6
JavaOOP_Lab6
Lab 6. OOP
The class DiscountRate contains only static variables and methods (underlined in the class
diagram).
1
HaQT Object-Oriented Programming
A polyline is a line with segments formed by points. Let’s use the ArrayList (dynamically
allocated array) to keep the points, but upcast to List in the instance variable. (Take note
that array is of fixed-length, and you need to set the initial length).
1 p u b l i c c l a s s Po in t {
private int x ;
3 private int y ;
p u b l i c P oi nt ( i n t x , i n t y ) { . . . . . . }
5 p u b l i c i n t getX ( ) { . . . . . . }
p u b l i c i n t getY ( ) { . . . . . . }
7 p u b l i c v o i d setX ( i n t x ) { . . . . . . }
p u b l i c v o i d setY ( i n t y ) { . . . . . . }
9 p u b l i c i n t [ ] getXY ( ) { . . . . . . }
p u b l i c v o i d setXY ( i n t x , i n t y ) { . . . . . . }
11 public String toString () { . . . . . . }
p u b l i c d o u b l e d i s t a n c e ( Po in t a n o t h e r ) { . . . . . . }
13 }
1 import j a v a . u t i l . ∗ ;
p u b l i c c l a s s PolyLine {
3 p r i v a t e L i s t <Point> p o i n t s ; // L i s t o f P oi nt i n s t a n c e s
5 // C o n s t r u c t o r s
p u b l i c P o l y L i n e ( ) { // d e f a u l t c o n s t r u c t o r
7 p o i n t s = new A r r a y L i s t <Point >() ; // implement with A r r a y L i s t
}
9 p u b l i c P o l y L i n e ( L i s t <Point> p o i n t s ) {
this . points = points ;
11 }
2
HaQT Object-Oriented Programming
13 // Append a p o i n t ( x , y ) t o t h e end o f t h i s p o l y l i n e
p u b l i c v o i d appendPoint ( i n t x , i n t y ) {
15 P oint newPoint = new P oi nt ( x , y ) ;
p o i n t s . add ( newPoint ) ;
17 }
19 // Append a p o i n t i n s t a n c e t o t h e end o f t h i s p o l y l i n e
p u b l i c v o i d appendPoint ( Po in t p o i n t ) {
21 p o i n t s . add ( p o i n t ) ;
}
23
// Return { ( x1 , y1 ) ( x2 , y2 ) ( x3 , y3 ) . . . . }
25 public String toString () {
// Use a S t r i n g B u i l d e r t o e f f i c i e n t l y b u i l d t h e r e t u r n S t r i n g
27 S t r i n g B u i l d e r sb = new S t r i n g B u i l d e r ( ”{ ” ) ;
f o r ( Point a Point : p o i n t s ) {
29 sb . append ( aP oint . t o S t r i n g ( ) ) ;
}
31 sb . append ( ” } ” ) ;
r e t u r n sb . t o S t r i n g ( ) ;
33 }
35 // Return t h e t o t a l l e n g t h o f t h i s p o l y l i n e
p u b l i c d o u b l e get L eng t h ( ) { . . . . . . }
37 }
1 /∗
∗ A Test D r i v e r f o r t h e P o l y L i n e c l a s s .
3 ∗/
import j a v a . u t i l . ∗ ;
5 public c l a s s TestPolyLine {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
7 // Test d e f a u l t c o n s t r u c t o r and t o S t r i n g ( )
P o l y L i n e l 1 = new P o l y L i n e ( ) ;
9 System . out . p r i n t l n ( l 1 ) ; // {}
11 // Test appendPoint ( )
l 1 . appendPoint ( new Point ( 1 , 2 ) ) ;
13 l 1 . appendPoint ( 3 , 4 ) ;
l 1 . appendPoint ( 5 , 6 ) ;
15 System . out . p r i n t l n ( l 1 ) ; // { ( 1 , 2 ) ( 3 , 4 ) ( 5 , 6 ) }
17 // Test c o n s t r u c t o r 2
L i s t <Point> p o i n t s = new A r r a y L i s t <Point >() ;
19 p o i n t s . add ( new Point ( 1 1 , 1 2 ) ) ;
p o i n t s . add ( new Point ( 1 3 , 1 4 ) ) ;
21 P o l y L i n e l 2 = new P o l y L i n e ( p o i n t s ) ;
3
HaQT Object-Oriented Programming
System . out . p r i n t l n ( l 2 ) ; // { ( 1 1 , 1 2 ) ( 1 3 , 1 4 ) }
23 }
}