Public class shoppingitem private string

This preview shows page 14 - 17 out of 18 pages.

public class ShoppingItem { private String quantityOrWeight; private String itemDescription; public ShoppingItem(String shoppingLine) { splitShoppingLine(shoppingLine); } public void splitShoppingLine(String shoppingLine) { int spacePosition; spacePosition = shoppingLine.indexOf(' '); quantityOrWeight = shoppingLine.substring( 0, spacePosition); itemDescription = shoppingLine.substring( spacePosition+1, shoppingLine.length()); quantityOrWeight=quantityOrWeight.toUpperCase(); itemDescription=itemDescription.toUpperCase(); } (10 marks) public void printItem() { System.out.println(quantityOrWeight + " - " + itemDescription); } } An example of the use of this class is shown in the code below. ShoppingItem item1 = new ShoppingItem("1 Loaf of white bread"); ShoppingItem item2 = new ShoppingItem("5kg Rua potatoes"); item1.printItem(); item2.printItem(); In the example above the following output would be produced: 1 - LOAF OF WHITE BREAD 5KG - RUA POTATOES
Image of page 14

Subscribe to view the full document.

Question/Answer Sheet - Page 15 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 11 (10 marks) The applet below provides a mouse driven interface for the battleships game. In this game the user selects a point on the screen which will either be a hidden ship, or the sea. The locations of the ships are arranged in an 8 x 8 grid. There is an array called layout which stores the position of the hidden ships, and a second array called shots which records whether a shot was fired into a particular grid position. When the game first starts, the applet is blank as in the screen shot to the right. Whenever the mouse is pressed, the corresponding grid location is drawn in either red or blue, depending on whether that grid location corresponds to a ship or the sea. An example of the game in play is shown in the following series of screenshots, where the darker rectangles (red) represent a hit ship and the lighter rectangles (blue) are missed shots into the sea: You must complete the paint() method for this applet. You will need to cycle through all of the elements in the shots array. For each element that is true , which indicates a shot has been fired, you need to draw a rectangle in the corresponding grid location. The colour of the rectangle should be red if the corresponding element of the layout array represents a ship, or blue if the corresponding element of the layout array represents the sea. The GRID_SIZE constant determines how wide and high the grid rectangle should be drawn. Take care with X (for columns) and Y (for rows) positions in respect to the shots array.
Image of page 15
Question/Answer Sheet - Page 16 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED import java.awt.*; import java.applet.*; import java.awt.event.*; public class Battleship extends Applet implements MouseListener { static final int GRID_SIZE = 15; // Size that grids are drawn static final int X_DIMENSION = 8; // X dimension for the arrays static final int Y_DIMENSION = 8; // Y dimension for the arrays static final int SEA = 0; // Value for a sea position static final int SHIP = 1; // Value for a ship position // Array containing positions of ships and sea static final int[][] layout = { {SEA, SEA, SEA, SEA, SEA, SEA, SEA, SEA}, {SEA, SEA, SEA, SEA,
Image of page 16

Subscribe to view the full document.

Image of page 17
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, ........., University Of Auckland

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern