Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Display trucks package-Boxed trucks package-FlatBed TODO: trucks package -mainprogram TODO: trucks package-Truck ASSESSMENT You will be assessed for your competence in programming and writing

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

class Display

image text in transcribedtrucks package-Boxed

image text in transcribedimage text in transcribedtrucks package-FlatBed

image text in transcribedimage text in transcribedTODO: trucks package -mainprogram

image text in transcribedimage text in transcribedTODO: trucks package-Truck

image text in transcribed

ASSESSMENT You will be assessed for your competence in programming and writing effective code. The purpose of all coding exercises in this course is the make your code more: modular programming design: you should at all times seek to achieve a modular programming design when submitting your work for grading (severe deductions apply otherwise). readable : your code should read like a story to a programmer. If I can't see the big picture of what your code is doing quickly, then its not readable. Comments should be used sparingly, identifier names and method implementations should strive to be so readable that comments are not necessary. understandable: you should always consider if your code "understandable to a 10 year old programmer?". clean: precise and to the point (no extra fuss...with in reason of course), one liners if it lends to higher level of readability and understanding. efficient: a corner stone of good programming, short to the point, and the use of time (run time) and space (memory) efficiency. CODE FORMAT Each class listed must be given in the following order: 1. constants (grouped/alphabetical order) 2. data fields (grouped/alphabetical order) 3. constructors in order of increasing parameters) 4. methods (alphabetical order) 5. inner classes (alphabetical order) GUI/ Inheritance About Objective: To demonstrate inheritance, polymorphism, and interfaces using Graphical User Interfaces (GUIS). You should assume that all data is encapsulated. Classes for Use You may ONLY use the classes in the Java API listed below or in the starter code. All imported classes or interfaces must be spelled out by name, DO NOT use the wildcard feature" * ". import java.awt.Color; import java.awt.Graphics; Trucks Instructions: Within the trucks package of the repository folder, you are given starter code with classes Truck, Boxed "Truck", FlatBed "Truck", Display and Main Program, where one or more inherits common behavior from Truck. NOTE: You will be using the class Display from the exercise package which is a basic GUI window "Frame" to help you draw the graphics of the Truck shape. Update the classes in the trucks package to display the information described below. Expectations Feel free to improve upon the code to make it more reusable/efficient. You are expected to play around with the location of graphical objects in the design, certain computer GUls may view the objects differently, so you may have to adjust the image based on your computer's interpretation of the window's coordinate space. to work out which hierarchy is best to use for this multi-class program (Basic Inheritance, Abstract Class or Interface). to incorporate reusable code where necessary and not to repeat code in certain places. to fill in the accessibility of methods and fields to run everything from the type "Truck", all features of the Truck, such as the "kind of Truck" should also be accessible through this type. Task List Update the starter code within each of the classes given with the methods given below. NOTE: You should be able to "draw" or "draw to scale" the graphics object within Truck for specific kinds of trucks using the operations "draw" or "drawToScale". This means that you should be able to use the "Truck" type to draw a "Flatbed" or "Boxed" Truck without reproducing redundant code. Update the TODO's within each class and answer the questions (if any) in your lab report. Update the Truck, Boxed and FlatBed classes with the proper Type Hierachy. Update the class constructor of Truck, Boxed and FlatBed so that it prints "Truck >", "Boxed" and "Flat Bed" respectively. Update your "Main Program", to make a "Truck" type that can be both a "Boxed Truck" or a "Flat Bed Truck" using the same reference, to demonstrate polymorphism. Each object must take a graphics object object as an argument. Your graphics object reference is "pen". update the method "testBoxed Truck" to draw the truck at the location (50, 100) o move the truck down by 100 (pixel units) resize(drawToScale) the truck by 200, 250, or 300 at the location (250, 50). You only need one. update the method "testFlatbed Truck" to o draw the truck at the location (50,315) o move the truck down by 100 (pixel units) o resize(drawToScale) the truck by 200, 250, or 300 at the location (250, 300). You only need one. NOTE: Feel free to adjust coordinates above to better suit the position in your GUI window. place SCREEN SHOTS of output, a brief summary and descriptions in your lab report PDF. A2A ... 9 Display.java 1 package exercise; 2 3 Eimport 6 7 public class Display { 8 public static final int WIDTH = 500; 10 public static final int HEIGHT = 400; 11 public static final int X_TOP = 200; 12 public static final int Y_TOP = 100; 13 14 public JFrame frame; //Display Window 15 16 public Display() { this(WIDTH, HEIGHT); } 19 public Display(int width, int height) { 21 frame = new JFrame(); 22 frame.setTitle("Drawing Window"); 23 frame.setSize(width, height); 24 frame.setLocation(X_TOP, Y_TOP); 25 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 frame.setVisible(true); 27 A 28 29 public Graphics getGraphics() { return frame.getContentPane().getGraphics(); } 32 33 public void clear() { frame.repaint(); } 36 37 public void setBackground (Color c) { frame.getContentPane ().setBackground (c); } 40 41 public void pause(int milliseconds) throws Exception { 42 Thread.sleep(milliseconds); 20 13 2 2 Display.java Boxed.java package trucks; 42 A5 A Elimport public class Boxed { // TODO: Update Class, you are expected to figure this out! public void draw(){ int outerXLeft = startX + 10; int outer/Right = startX + 70; int outery = starty + 40; int innerxLeft = startX + 15; int innerXRight = startX + 75; int innery = starty + 45; int windowX = startX + 70; int windowy = starty + 10; drawBody( width: 100, height: 50); drawwheel(outerxLeft, outery, innerXLeft, innery, 20); drawWheel(outerXRight, outery, innerXRight, innery, 20); drawWindow(windowx, windowy, 30, 20); // left wheel // right wheel } private void drawBody(int width, int height) { pen.setColor(color); pen.fillRect(startx, starty, width, height); } o public void drawtoScale(int length) { setSize(length); int outerXLeft = startX + size / 10; int outerXRight = startX + 7 * size / 10; int outery = starty + 2 * size / 5; int innerxLeft = startX + 3* size / 20; int innerXRight = startX + 3 * size / 4; int innery = starty + 9* size / 20; int window = startX + 7 * size / 10; int windowY = starty + size / 10; int windowWidth = 3 * size / 10; // x for left wheel // x for right wheel Il y for wheels // x for left hubcap // x for right hubcap // y for hubcaps 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 drawBody(size, height: size / 2); drawWheel(outerXLeft, outery, innerXLeft, innery, size / 5); drawWheel(outerXRight, outery, innerXRight, innery, size / 5); drawWindow(windowx, windowy, windowWidth, size / 6); A } } Flatbed.java 53 A5 AV * Ant Database 8 Display.java Boxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Pimport java.awt.Color; 4. Eimport java.awt.Graphics; 5 6 public class FlatBed { 7 //TODO: Update Class, you are expected to figure this out! 9 10 E public void draw(){ 11 int outerXLeft = startX + 10; 12 int outerXRight = startX + 70; 13 int outery = starty + 5; 14 int innerXLeft = startx + 15; 15 int inner/Right = startX + 75; 16 int innery = starty + 10; 17 int windowX = startX + 60; 18 int windowY = starty - 15; 19 20 drawFlatBedBody(startx starty); 21 drawWheel (outerXLeft, outery, inner/Left, innery, 20); 22 drawWheel(outerXRight, outery, innerXRight, innery, 20); 23 drawWindow(windowx, windowy, 30, 15); 24 } 25 26 private void drawFlatbedBody(int x, int y){ 27 pen.setColor(color); 28 pen.fillRect(x, y, 100, 15); 29 pen.fillRect(x + 50, y - 20, 50, 30); 30 } 31 // left wheel Il right wheel private void drawResizedBody(int x, int y){ pen.setColor(color); pen.fillRect(x, y, size, size / 6); pen.fillRect(x + size / 2, y - size 1 4, size / 2, 7 * size / 20); A public void drawToScale(int length) { 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 setSize(length); int outerXLeft = startX + size / 10; int outer/Right = startX + 7 * size / 10; int outery = starty + size | 20; int innerXLeft = startX + 3 * size / 20; int innerXRight = startX + 3 * size / 4; int innery = starty + size / 10; int windowX = startX + 6 * size / 10; int windowy = starty - 3* size / 20; int windowWidth = 7 * size | 20; Il x for left wheel // x for right wheel Il y for wheels // x for left hubcap Il x for right hubcap // y for hubcaps 49 50 51 52 53 54 55 56 57 58 drawResizedBody(startx, starty); drawWheel(outerXLeft, outery, innerXLeft, innery, size / 5); drawWheel(outerXRight, outery, innerXRight, innery, size / 5); drawWindow(windowx, windowy, windowWidth, size / 6); } Flatbed.java A6 AV W Database Display.java Boxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Eimport 6 7 public class MainProgram { 8 public static void testBoxedTruck (Display display, Truck truck) throws Exception{ 10 // Pause for a bit 11 display.pause ( milliseconds: 200); 12 13 El // TODO : draw truck at specific location 14 15 // TODO : move truck to a different location and draw 16 // TODO : resize Truck at specific location 18 19 // Pause for a bit 20 display.pause ( milliseconds: 200); 21 } 22 23 @ public static void testFlatbedTruck(Display display, Truck truck) throws Exception{ 17 A A 24 25 // Pause for a bit display.pause ( milliseconds: 200); 26 // TODO : draw truck at specific location 27 28 29 30 31 32 33 34 // TODO : move truck to a different location and draw // TODO : resize Truck at specific location A // Pause for a bit // TODO : resize Truck at specific location // Pause for a bit display.pause( milliseconds: 200); } public static void main(String[] args) throws Exception { 32 33 34 35 36 A 37 38 D 39 40 41 42 43 44 45 46 Display display = new Display( width: 600, height: 500); display.setBackground (Color.GRAY); // Get Graphics Pen Object for Drawing in Window Graphics pen = display.getGraphics(); // TODO : Create a Boxed Truck for display // clear graphics (if you want to Il display.clear(); 48 49 50 51 52 53 54 55 // TODO : with same reference, to create a Flatbed Truck for display } A } Flatbed.java nu A6 A Display.javaBoxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Eimport 5 6 public class Truck { 7 8 protected Color color; 9 protected Graphics pen; 10 protected int size; 11 protected int startx; 12 protected int startY; 13 //TODO: Update Class, you are expected to figure this out! 15 16 E protected void drawWheel(int outerx, int outery, int innerx, int innery, int length) { 17 pen.setColor(Color.BLACK); 18 pen.filloval (outerx, outery, length, length); pen.setColor(Color.LIGHT_GRAY); 20 pen.filloval(innerx, innery, width: length / 2, height: length / 2); 21 22 } 14 19 23 O 24 25 26 protected void drawWindow(int x, int y, int width, int height) { pen.setColor(Color.LIGHT_GRAY); pen.fillRect(x, y, width, height); 27 28 29 ASSESSMENT You will be assessed for your competence in programming and writing effective code. The purpose of all coding exercises in this course is the make your code more: modular programming design: you should at all times seek to achieve a modular programming design when submitting your work for grading (severe deductions apply otherwise). readable : your code should read like a story to a programmer. If I can't see the big picture of what your code is doing quickly, then its not readable. Comments should be used sparingly, identifier names and method implementations should strive to be so readable that comments are not necessary. understandable: you should always consider if your code "understandable to a 10 year old programmer?". clean: precise and to the point (no extra fuss...with in reason of course), one liners if it lends to higher level of readability and understanding. efficient: a corner stone of good programming, short to the point, and the use of time (run time) and space (memory) efficiency. CODE FORMAT Each class listed must be given in the following order: 1. constants (grouped/alphabetical order) 2. data fields (grouped/alphabetical order) 3. constructors in order of increasing parameters) 4. methods (alphabetical order) 5. inner classes (alphabetical order) GUI/ Inheritance About Objective: To demonstrate inheritance, polymorphism, and interfaces using Graphical User Interfaces (GUIS). You should assume that all data is encapsulated. Classes for Use You may ONLY use the classes in the Java API listed below or in the starter code. All imported classes or interfaces must be spelled out by name, DO NOT use the wildcard feature" * ". import java.awt.Color; import java.awt.Graphics; Trucks Instructions: Within the trucks package of the repository folder, you are given starter code with classes Truck, Boxed "Truck", FlatBed "Truck", Display and Main Program, where one or more inherits common behavior from Truck. NOTE: You will be using the class Display from the exercise package which is a basic GUI window "Frame" to help you draw the graphics of the Truck shape. Update the classes in the trucks package to display the information described below. Expectations Feel free to improve upon the code to make it more reusable/efficient. You are expected to play around with the location of graphical objects in the design, certain computer GUls may view the objects differently, so you may have to adjust the image based on your computer's interpretation of the window's coordinate space. to work out which hierarchy is best to use for this multi-class program (Basic Inheritance, Abstract Class or Interface). to incorporate reusable code where necessary and not to repeat code in certain places. to fill in the accessibility of methods and fields to run everything from the type "Truck", all features of the Truck, such as the "kind of Truck" should also be accessible through this type. Task List Update the starter code within each of the classes given with the methods given below. NOTE: You should be able to "draw" or "draw to scale" the graphics object within Truck for specific kinds of trucks using the operations "draw" or "drawToScale". This means that you should be able to use the "Truck" type to draw a "Flatbed" or "Boxed" Truck without reproducing redundant code. Update the TODO's within each class and answer the questions (if any) in your lab report. Update the Truck, Boxed and FlatBed classes with the proper Type Hierachy. Update the class constructor of Truck, Boxed and FlatBed so that it prints "Truck >", "Boxed" and "Flat Bed" respectively. Update your "Main Program", to make a "Truck" type that can be both a "Boxed Truck" or a "Flat Bed Truck" using the same reference, to demonstrate polymorphism. Each object must take a graphics object object as an argument. Your graphics object reference is "pen". update the method "testBoxed Truck" to draw the truck at the location (50, 100) o move the truck down by 100 (pixel units) resize(drawToScale) the truck by 200, 250, or 300 at the location (250, 50). You only need one. update the method "testFlatbed Truck" to o draw the truck at the location (50,315) o move the truck down by 100 (pixel units) o resize(drawToScale) the truck by 200, 250, or 300 at the location (250, 300). You only need one. NOTE: Feel free to adjust coordinates above to better suit the position in your GUI window. place SCREEN SHOTS of output, a brief summary and descriptions in your lab report PDF. A2A ... 9 Display.java 1 package exercise; 2 3 Eimport 6 7 public class Display { 8 public static final int WIDTH = 500; 10 public static final int HEIGHT = 400; 11 public static final int X_TOP = 200; 12 public static final int Y_TOP = 100; 13 14 public JFrame frame; //Display Window 15 16 public Display() { this(WIDTH, HEIGHT); } 19 public Display(int width, int height) { 21 frame = new JFrame(); 22 frame.setTitle("Drawing Window"); 23 frame.setSize(width, height); 24 frame.setLocation(X_TOP, Y_TOP); 25 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 frame.setVisible(true); 27 A 28 29 public Graphics getGraphics() { return frame.getContentPane().getGraphics(); } 32 33 public void clear() { frame.repaint(); } 36 37 public void setBackground (Color c) { frame.getContentPane ().setBackground (c); } 40 41 public void pause(int milliseconds) throws Exception { 42 Thread.sleep(milliseconds); 20 13 2 2 Display.java Boxed.java package trucks; 42 A5 A Elimport public class Boxed { // TODO: Update Class, you are expected to figure this out! public void draw(){ int outerXLeft = startX + 10; int outer/Right = startX + 70; int outery = starty + 40; int innerxLeft = startX + 15; int innerXRight = startX + 75; int innery = starty + 45; int windowX = startX + 70; int windowy = starty + 10; drawBody( width: 100, height: 50); drawwheel(outerxLeft, outery, innerXLeft, innery, 20); drawWheel(outerXRight, outery, innerXRight, innery, 20); drawWindow(windowx, windowy, 30, 20); // left wheel // right wheel } private void drawBody(int width, int height) { pen.setColor(color); pen.fillRect(startx, starty, width, height); } o public void drawtoScale(int length) { setSize(length); int outerXLeft = startX + size / 10; int outerXRight = startX + 7 * size / 10; int outery = starty + 2 * size / 5; int innerxLeft = startX + 3* size / 20; int innerXRight = startX + 3 * size / 4; int innery = starty + 9* size / 20; int window = startX + 7 * size / 10; int windowY = starty + size / 10; int windowWidth = 3 * size / 10; // x for left wheel // x for right wheel Il y for wheels // x for left hubcap // x for right hubcap // y for hubcaps 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 drawBody(size, height: size / 2); drawWheel(outerXLeft, outery, innerXLeft, innery, size / 5); drawWheel(outerXRight, outery, innerXRight, innery, size / 5); drawWindow(windowx, windowy, windowWidth, size / 6); A } } Flatbed.java 53 A5 AV * Ant Database 8 Display.java Boxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Pimport java.awt.Color; 4. Eimport java.awt.Graphics; 5 6 public class FlatBed { 7 //TODO: Update Class, you are expected to figure this out! 9 10 E public void draw(){ 11 int outerXLeft = startX + 10; 12 int outerXRight = startX + 70; 13 int outery = starty + 5; 14 int innerXLeft = startx + 15; 15 int inner/Right = startX + 75; 16 int innery = starty + 10; 17 int windowX = startX + 60; 18 int windowY = starty - 15; 19 20 drawFlatBedBody(startx starty); 21 drawWheel (outerXLeft, outery, inner/Left, innery, 20); 22 drawWheel(outerXRight, outery, innerXRight, innery, 20); 23 drawWindow(windowx, windowy, 30, 15); 24 } 25 26 private void drawFlatbedBody(int x, int y){ 27 pen.setColor(color); 28 pen.fillRect(x, y, 100, 15); 29 pen.fillRect(x + 50, y - 20, 50, 30); 30 } 31 // left wheel Il right wheel private void drawResizedBody(int x, int y){ pen.setColor(color); pen.fillRect(x, y, size, size / 6); pen.fillRect(x + size / 2, y - size 1 4, size / 2, 7 * size / 20); A public void drawToScale(int length) { 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 setSize(length); int outerXLeft = startX + size / 10; int outer/Right = startX + 7 * size / 10; int outery = starty + size | 20; int innerXLeft = startX + 3 * size / 20; int innerXRight = startX + 3 * size / 4; int innery = starty + size / 10; int windowX = startX + 6 * size / 10; int windowy = starty - 3* size / 20; int windowWidth = 7 * size | 20; Il x for left wheel // x for right wheel Il y for wheels // x for left hubcap Il x for right hubcap // y for hubcaps 49 50 51 52 53 54 55 56 57 58 drawResizedBody(startx, starty); drawWheel(outerXLeft, outery, innerXLeft, innery, size / 5); drawWheel(outerXRight, outery, innerXRight, innery, size / 5); drawWindow(windowx, windowy, windowWidth, size / 6); } Flatbed.java A6 AV W Database Display.java Boxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Eimport 6 7 public class MainProgram { 8 public static void testBoxedTruck (Display display, Truck truck) throws Exception{ 10 // Pause for a bit 11 display.pause ( milliseconds: 200); 12 13 El // TODO : draw truck at specific location 14 15 // TODO : move truck to a different location and draw 16 // TODO : resize Truck at specific location 18 19 // Pause for a bit 20 display.pause ( milliseconds: 200); 21 } 22 23 @ public static void testFlatbedTruck(Display display, Truck truck) throws Exception{ 17 A A 24 25 // Pause for a bit display.pause ( milliseconds: 200); 26 // TODO : draw truck at specific location 27 28 29 30 31 32 33 34 // TODO : move truck to a different location and draw // TODO : resize Truck at specific location A // Pause for a bit // TODO : resize Truck at specific location // Pause for a bit display.pause( milliseconds: 200); } public static void main(String[] args) throws Exception { 32 33 34 35 36 A 37 38 D 39 40 41 42 43 44 45 46 Display display = new Display( width: 600, height: 500); display.setBackground (Color.GRAY); // Get Graphics Pen Object for Drawing in Window Graphics pen = display.getGraphics(); // TODO : Create a Boxed Truck for display // clear graphics (if you want to Il display.clear(); 48 49 50 51 52 53 54 55 // TODO : with same reference, to create a Flatbed Truck for display } A } Flatbed.java nu A6 A Display.javaBoxed.java MainProgram.java Truck.java 1 package trucks; 2 3 Eimport 5 6 public class Truck { 7 8 protected Color color; 9 protected Graphics pen; 10 protected int size; 11 protected int startx; 12 protected int startY; 13 //TODO: Update Class, you are expected to figure this out! 15 16 E protected void drawWheel(int outerx, int outery, int innerx, int innery, int length) { 17 pen.setColor(Color.BLACK); 18 pen.filloval (outerx, outery, length, length); pen.setColor(Color.LIGHT_GRAY); 20 pen.filloval(innerx, innery, width: length / 2, height: length / 2); 21 22 } 14 19 23 O 24 25 26 protected void drawWindow(int x, int y, int width, int height) { pen.setColor(Color.LIGHT_GRAY); pen.fillRect(x, y, width, height); 27 28 29

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago