4
MUST — ICS — OOP Basic Object-Oriented Programming in Java Exercises John Businge & Richard Kimera February 22, 2013 These exercises will get you acquainted with basic java syntax. Before you can start with the exercises, you can first play with the code fragments presented with this assignment in the Java project (folder) called basic-oop-examples. First unzip the file frombasic-oop-examples. zip. After playingwith these examples, you will knowwhat a typical Java code for solving a given problem looks like. Some of these exercises we already went through together in last lecture. 1 EXAMPLES CLASSES TO PLAY WITH As usual, open Eclipse and open the project basic-oop- examples. Look at these example classes and play with them. These are the same classes we discussed during the lecture.

Java assgnmt2

Embed Size (px)

Citation preview

Page 1: Java assgnmt2

MUST — ICS — OOPBasic Object-Oriented Programming in JavaExercises

John Businge & Richard KimeraFebruary 22, 2013

These exercises will get you acquainted with basic java syntax. Before you can start with theexercises, you can first play with the code fragments presented with this assignment in theJava project (folder) called basic-oop-examples. First unzip the file frombasic-oop-examples.zip. After playingwith these examples, you will knowwhat a typical Java code for solvinga given problem looks like. Some of these exercises we already went through together in lastlecture.1 EXAMPLES CLASSES TO PLAY WITHAs usual, open Eclipse and open the project basic-oop-examples. Look at these exampleclasses and play with them. These are the same classes we discussed during the lecture.2 THE EXERCISES1. Make a new Eclipse project called â˘AIJshapes1â˘A˙I. Create a Circle class that contains aradius field. Give it a constructor where you pass in the radius. Have your test routine

Page 2: Java assgnmt2

create a few circles, assign a value to the radius, then print out some information aboutthe circles. Put your Circle class and your test routine in two separate classes, like this:• Circle.javapublic clas s Ci rcle {public double radius ;. . .}1• CircleTest.javapublic clas s Ci rcleTest {public s t a t i c void main( St r ing [ ] args ) {Ci rcle c = new Ci rcle ( . . . ) ;. . . }}

Notes

• The Circle class does not have a main, so you cannot execute it directly. You onlydirectly run the CircleTest class (R-click, Run As, Java Application).• This problem requires several different capabilities. Unless you have previousJava experience, I strongly recommend you build up to the solution in a piecemealfashion. Firstmake a Circle class with a radius field only, and test it out fromthe main in CircleTest. Then add in a constructor. Then test it out again. And soon. Use the four Person examples from the end of the lecture as a model for bothyour code and the iterative development/testing process.

2. Give your Circle a getArea method that calculates its area, and a printInfo method thatprints out the radius and area. Make a test case that tries these capabilities out.

Page 3: Java assgnmt2

3. Make a program that creates an array of 100 circles, each with a random radius. Print out the sum of the areas of the circles. Also print the biggest and smallest areas. Hint:remember that in the two-step array allocation process, the following line only makesspace for 100 circles (or, more technically, it allocates an array of 100 null Circle pointers),it does not create any circles:Ci rcle [ ] c i r c l e s = new Ci rcle [ 10 0 ] ;To actually create the circles, you have to do a loop:for ( int i =0; i <c i r c l e s . length ; i ++) {c i r c l e s [ i ] = new Circle ( . . . ) ;}

4. Create a Rectangle class that contains width and height fields. Also give it a getAreamethod. Again,make a few test cases.

5. Create a Square classwithwidth and getArea. Then, give both Square and Circle setAreamethods that let you specify a desired area. Make a few test cases.

ONE WEEK FROM THE DATE YOU RECEIVED THE ASSIGNMENT2.