ACS-1903 Lab 2: Objects, Scanners, Numbers Fall 2007 Working with Objects, Scanners, Numbers In Lab 2 we will work with the same shapes classes as in Lab1, but we will interact with them programmatically using main() and appropriate messages. In this lab you will : Review the shapes classes: Circle, Square, Triangle Write classes with main() methods that interact with shapes objects The lab demonstrator will initial your lab appropriately in the spaces provided, and when you are finished, hand the lab in to your demonstrator. Student Identification Student number ____________________________________________ Last name ____________________________________________ First name ____________________________________________ Workstation user id ____________________________________________ Email address ____________________________________________ 1. Logon to your workstation Using your userid and password from Lab 1, logon to a workstation. Obtain the Java files Begin with the same Java classes as in Lab1. If necessary you can download the four java files from http://io.uwinnipeg.ca/~rmcfadye/1903/shapes/. Start BlueJ Create a BlueJ Project Create a project. It can be useful to you if you store such a project on the H drive. You have access to this drive from both computer labs. Import java files If necessary, import the four java files to your project Review the Triangle, Circle and Square classes Use Tools >> Project Documentation to review the methods/responsibilities of these classes. 1 ACS-1903 Lab 2: Objects, Scanners, Numbers Fall 2007 2. In Lab 1 you determined how to arrange for a square to be immediately below a triangle. Now do this programmatically using a main() method. Your main() method must: Instantiate a triangle, Instantiate a square, Make the triangle visible Make the square visible Send one or more move messages to the triangle and/or square The following code may prove useful to you; it does all but the movement. You can create a new class in your project named drawPicture, paste the following in it, test it and develop it further. public class drawPicture { public static void main() { Triangle t; Square s; t = new Triangle(); s = new Square(); t.makeVisible(); s.makeVisible(); // place one or more move messages here } } Note that when testing your program (modify and recompile), you may need to reset the Java Virtual Machine. When you have drawPicture working correctly you must show it to the lab demonstrator. Demonstrator’s initials: 2 ACS-1903 Lab 2: Objects, Scanners, Numbers Fall 2007 3. Note that each of Triangle, Square, Circle have methods to move horizontally and vertically a number of pixels: public void moveHorizontal(int distance) public void moveVertical(int distance) Write a class named moveObjects with a main method that uses the Scanner class and the nextInt() method to accomplish: prompt for a horizontal distance and send moveHorizontal() to a square prompt for a vertical distance and send moveVertical() to a square prompt for a horizontal distance and send moveHorizontal()to a circle prompt for a vertical distance and send the moveVertical()to a circle prompt for a horizontal distance and send moveHorizontal()to a triangle prompt for a vertical distance and send the moveVertical()to a triangle You may find the following code a useful starting point. import java.util.Scanner; /** * Moving shapes objects left/right and up/down * * @author (your name) * @version (a version number or a date) */ public class moveObjects { public static void main() { Square s; s = new Square(); s.makeVisible(); System.out.println( "How many pixels should I move the square?"); Scanner keyboard = new Scanner(System.in); int moveSquareHorizontal = keyboard.nextInt(); s.moveHorizontal(moveSquareHorizontal); } } Note that when testing your program (modify and recompile), you may need to reset the Java Virtual Machine. When you have moveObjects working correctly you must show it to the lab demonstrator. Demonstrator’s initials: 3