Continued question 6 10 marks complete the board

This preview shows page 10 - 14 out of 24 pages.

CONTINUED Question 6 (10 marks) Complete the Board class below which is used to play the Connect Four game as required in Assignment 04. The Board class uses a single String (called data ) to represent the positions occupied by counters in an N*N board used to play Connect Four. Write a method called setValue() which is used to set the value at a given ( x,y ) position in the Board to a given character. Write a second method called add() which is used to add a value to a given column. The value added to the column should "fall" down to the bottom of that column (i.e. it will be placed in the lowest empty position in the given column). public class Board { public static final char YELLOW='X'; public static final char RED='O'; public static final char EMPTY='.'; private String data; // Each position in the board is // represented by a single // character private int size; // The height and width of the // board public Board(int n) { size = n; data = ""; for(int i=0; i<n; i++){ for(int j=0;j<n;j++) data+=EMPTY; } } public char getValue(int x, int y) { return data.charAt(y*size+x); } // Sets the value at a given (x,y) position on the board. public void setValue( int x, int y, char c ) { data = data.substring(0, y * size + x) + c + data.substring(y * size + x + 1); (3 marks) } // Adds the given character to the board. The character // will be added to the lowest empty position in the // column specified public void add( int x, char c ) { for(int i = size - 1;i >= 0; i--){ if(getContent(x,i) == EMPTY){ setContent(x,i,c); break; } } (7 marks) } }
Image of page 10

Subscribe to view the full document.

Question/Answer Sheet - Page 11 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 7 (5 marks) The following application makes use of a Car class. Read the application carefully and look at the output which is produced. You must complete the Car class so that the code below will produce the correct results. public class TestCar { public static void main(String[] args){ Car c1 = new Car("Honda", "Blue", 1978); Car c2 = new Car("Mini", "Red", 1974); System.out.println(c1); c1.drive(2000); System.out.println(c1); c1.drive(3000); System.out.println(c1); c2.drive(5043); if(c1.isOlderThan(c2)) System.out.println(c1); else System.out.println(c2); } } When the application above is executed, the output below is produced. Blue 1978 Honda. Has travelled: 0kms. Blue 1978 Honda. Has travelled: 2000kms. Blue 1978 Honda. Has travelled: 5000kms. Red 1974 Mini. Has travelled: 5043kms.
Image of page 11
Question/Answer Sheet - Page 12 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Complete the Car class in the space provided: public class Car { private int year, milage; private String make, colour; public Car(String make, String colour, int year){ this.make = make; this.colour = colour; this.year = year; this.milage = 0; } public String toString(){ String result = colour + " " + year + " " + make + ". Has travelled: " + milage + "kms."; return result; } public void drive(int d){ milage += d; } public boolean isOlderThan(Car c){ return year < c.year; } } (5 marks)
Image of page 12

Subscribe to view the full document.

Question/Answer Sheet - Page 13 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 8 (11 marks) Consider the following JPanel class carefully: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyJPanel extends JPanel implements ActionListener { private JButton b1, b2, b3; private int number; public MyJPanel() { setBackground(Color.white); number = 10; b1 = new JButton("One"); b2 = new JButton("Two"); b3 = new JButton("Three"); b1.addActionListener(this); b3.addActionListener(this); add(b3); add(b2); add(b1); }
Image of page 13
Image of page 14
You've reached the end of this preview.

{[ 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