User Tools

Site Tools


cs_176:501b_assignments

Back to CS-176/501B Home


List of CS-176/501b Assignments


Note: Unless otherwise instructed, use BlueJ to complete the assignments. Save all work under your cs176 or cs501b folder in your school UNIX account.

  • Information on remotely accessing your school UNIX account is given under the “Links and Resources” section of the course web page.

Assignment 1: Two Dimensional Arrays

Deadline:

Save all work under the folder cs176/Assignment1/ (or cs501b/Assignment1/) in your UNIX home folder.

1. SumMatrix.java: Sum all the numbers in a matrix

Write and test a method that sums all the integers in a matrix of integers. Use 1_2_4_5_6_7_8_9_10_11_12_13_14_15_16_17 to test the method.

  • As the instructions state, use a summing method that you call from the main() method.
  • The method should be able to handle 2D matrices of any size.

Submit the SumMatrix.java file only.

2. SumMatrixDiag.java: Sum the major diagonal in a matrix

Write and test a method that sums all the integers in the major diagonal in an n x n matrix of integers. Use 1_2_4_5_6_7_8_9_10_11_12_13_14_15_16_17 to test the method.

  • A major diagonal is the diagonal formed from the top left corner to the bottom right corner of the matrix.
  • The method you write should be able to handle 2D matrices of any size, as long as it's a square matrix (rows=columns).

Submit the SumMatrixDiag.java file only.

3. T3Board.java: TicTacToe board

Write a program that randomly fills in 0s and 1s into a TicTacToe board, prints the board, and finds the rows, columns, or diagonals with all 0s or 1s. Use a two-dimensional array to represent a TicTacToe board. Here is a sample run of the program:

      
      001
      001
      111
      All 1's on row 2
      All 1's on column 2

Submit the T3Board.java file only.


Assignment 2: Arrays

Deadline:

Save all work under the folder cs176/Assignment2/ (or cs501b/Assignment2/) in your UNIX home folder.

Note: Indent your code properly or use the bluej auto-layout feature.

1. BubbleSort 1D String Array

Save and submit as BubbleSortString.java.

See some online notes on 1D array bubble sorts.

Complete the following method that sorts an array of Strings in ascending lexicographical order.

  public static void bubbleSortString(String[] s)
  {
  
  }

Include a main method that creates an unsorted one-dimensional array of Strings and uses the bubbleSortString method to sort the string array. The main method should then output the sorted array of Strings. Use this array of Strings to test your method.

2. Locate Largest Element in 2D Array

  • Save and submit as LocateLargest.java.

Complete the following method that outputs the row and column location of the largest element in a two-dimensional array.

  public static void locateLargest(double[][] a)
  {
  
  }

Include a main method that creates a two-dimensional array of doubles and uses the locateLargest method to display the location of the largest element in the array. Here is a sample run:

    In the following array:
    23.5   35   2    10
    4.5    3    45   3.5
    35     44   5.5  9.6
    The largest element is 45 at row 2, column 3.

Assignment 3: Class Design

Deadline:

Save all work under the folder cs176/Assignment3/ (or cs501b/Assignment3/) in your UNIX home folder.

Note: Indent your code properly or use the bluej auto-layout feature.

1. The Fan Class

  • Create Fan.java and TestFan.java.
  • Use bluej-provided comment blocks to properly document the Fan class and its methods.
  • Design a class named Fan (Fan.java) to represent a fan. The class contains:
    • Three constants (final int) named SLOW, MEDIUM, and FAST with values 1, 2, and 3 to denote the fan speed.
    • An int data field named speed that specifies the speed of the fan (default SLOW).
    • A boolean data field named on that specifies whether the fan is on (default false).
    • A double data field named radius that specifies the radius of the fan (default 5).
    • A String data field named color that specifies the color of the fan (default blue).
    • A default (no-argument) constructor that creates a default fan.
    • The accessor (get) and mutator (set) methods for all four data fields.
    • A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns fan color and radius along with the string “fan is off” in one combined string.
  • Implement (test) the class.
    • Write a test program (TestFan.java) that creates two Fan objects.
      • To the first Fan object, assign maximum speed, radius 10, color yellow, and on status.
      • To the second Fan object, assign medium speed, radius 5, color blue, and off status.
      • Display the Fan objects by invoking their toString() methods.

2. Minesweeper Square class

  • Create MineSquare.java.
  • Use bluej-provided comment blocks to properly document the MineSquare class and its methods.
  • Design a Minesweeper Square class that represents a single square (or cell) in the Minesweeper game. Save it as MineSquare.java. The class should have the following attributes:
       private  String  _shown;      // What a square is showing now
       private  boolean _mined;      // Square is mined or not
       private  boolean _flagged;    // Square is flagged or not
       private  boolean _questioned; // Square is question marked or not
       private  int     _minecount;  // Square's surrounding mine count
       private  boolean _opened;     // Player has opened this square or not

Create all needed get and set methods for these variables.

The possible values for the _shown String variable would be:

  • “X” (a mine)
  • a mine count number between 1 and 8
  • “ ” (blank space) for mine count = 0
  • “F” (a flag)
  • “?” (question mark)

Think about how the MineSquare class variables might be related. For example, if a player puts a flag on a square, what would that square be showing? And, if a player opens a square, what would that square be showing?

You do not need to write a program to test the MineSquare class at this time.


Assignment 4: Objects from Methods, Objects in Classes

Deadline:

Save all work under the folder cs176/Assignment4/ (or cs501b/Assignment4/) in your UNIX home folder.

Note: Indent your code properly or use the bluej auto-layout feature. Also, comment your code to explain what you are doing.

1. Fraction2 class

Fraction add instance method

  • Save and dropbox Fraction2.java and TestFraction2.java.
  • Use the original Fraction class example as a starting point.
    • In the original Fraction class example, the addFraction and subtractFraction methods were static methods in TestFraction.
    • In Fraction2, make the addFraction and subtractFraction methods instance methods of the Fraction2 class.
  • This is the way we want to be able to work with Fraction2 Class objects:
            //
            // Code that goes in TestFraction2.java:
            //
            Fraction2 f1 = new Fraction2( 5, 8 );
            Fraction2 f2 = new Fraction2( 1, 9 );
            Fraction2 f3 = new Fraction2();
            
            // This time, addFraction is an instance method of the Fraction2
            // class, so we use the object f3's addFraction() method, i.e.,
            // f3.addFraction().
            f3.addFraction( f1, f2 ); // f3 holds the result of 5/8 + 1/9
            
            System.out.println( f1.printFraction() + " + " +
                                f2.printFraction() + " = " +
                                f3.printFraction() );

2. Summary of Commenting Guidelines

In at least 200 words, summarize in your own words the code commenting guidelines at http://tinyurl.com/ms5ev77.

Save your answers in a plain text file called Good_Comments.txt. Recommended plain text editors: jedit, notepad++.


Assignment 5: Class Design, Static Class Members

Deadline:

Save all work under the folder cs176/Assignment5/ (or cs501b/Assignment5/) in your UNIX home folder.

Note: Indent your code properly or use the bluej auto-layout feature. Also, comment your code to explain what you are doing.

1. Class Design Guidelines

In at least 200 words, summarize in your own words the class design guidelines given in the document that was emailed or handed out to you as part of this assignment. Be sure to include descriptions of what cohesion and coupling are.

Save your answers in a plain text file called Class_Design.txt. Do not submit a .docx file. Recommended plain text editors: jedit, notepad++. If you are typing this in UNIX, use the gedit text editor.

2. IntegerMatrix

  • Save IntegerMatrix.java and TestIntegerMatrix.java.
  • Design an IntegerMatrix class from the matrix programs we worked with in Assignment 1.
  • The IntegerMatrix class will contain only the following static methods:
    • the sumMatrix method to return the sum of an integer matrix
    • the sumDiag method to return the sum of an integer matrix's major diagonal
    • the locateLargest method that finds and returns the largest value in an integer matrix, and also the row and column for the largest value.
  • Test the 3 static methods of the IntegerMatrix class with TestIntegerMatrix.java.
    • There will no methods other than the main() method in TestIntegerMatrix.java.
    • Use the following 2D array in TestIntegerMatrix.java to test the methods:
        24   35   2    10
        5    3    45   4
        35   44   6    9
        8    15   25   11

Assignment 6: Inheritance, GUIs, Polymorphism

Deadline:

Save all work under the folder cs176/Assignment6/ (or cs501b/Assignment6/) in your UNIX home folder.

Note: Indent your code properly or use the bluej auto-layout feature. Also, comment your code to explain what you are doing.

1. Person, Student, Employee, Faculty and Staff Class Hierarchy

The BlueJ project you create should contain Person.java, Student.java, StudentYear.java, Employee.java, Faculty.java, Staff.java, and TestPersons.java.

Design a class named Person and its two subclasses named Student and Employee.

  • Make Faculty and Staff subclasses of Employee.
  • A Person has a name (String)…
    • …and also getname/setName methods plus a toString method that returns the Person's name.
  • A Student has a status variable which is set to one of the constants in the public enum StudentYear, defined below:
          // put this in StudentYear.java
          public enum StudentYear { FRESHMAN, SOPHMORE, JUNIOR, SENIOR }
  • An Employee has an office number (String), and salary (double).
  • A Faculty member has a rank (String).
  • A Staff member has a title (String).
  • Override the Person class toString() method in each subclass to display just the subclass name and the person's name.

Keep the class files simple and short by using inheritance. In all the subclasses (every class besides Person), the only method you define should be the toString method.

Use the following TestPersons class to test the above classes:

                // TestPersons.java:
                // creates Person, Student, Employee, Faculty, and Staff objects

                public class TestPersons {
                  public static void main(String[] args) {

                    Person pete = new Person();
                    pete.setName("Pete");
                    System.out.println(pete + "\n");

                    Student stan = new Student();
                    stan.setName("Stan");
                    System.out.println(stan + "\n");

                    Employee ernie = new Employee();
                    ernie.setName("Ernie");
                    System.out.println(ernie + "\n");

                    Faculty coddington = new Faculty();
                    coddington.setName("Coddington");
                    System.out.println(coddington + "\n");

                    Staff starr = new Staff();
                    starr.setName("Starr");
                    System.out.println(starr);
                  }
                }

2. FractionCalc

The BlueJ project you create should contain Fraction.java and FractionCalc.java.

Complete the FractionCalc GUI that we have been working on in class. Include a fourth button for the division operation. Yes, you will need to add a working divideFraction method to the Fraction class.

3. Polymorphism

Write your answers in Polymorphism.txt and save it.

Read http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html on the instanceof operator. Then answer the following questions about the GeometricObject1 and Triangle classes:

Given the following declarations:

         Triangle triangle = new Triangle();
         GeometricObject1 object1 = new GeometricObject1();

1) Are the following boolean expressions true or false?

         (triangle instanceof GeometricObject1)
         (object1 instanceof GeometricObject1)
         (triangle instanceof Triangle)              
         (object1 instanceof Triangle)             

2) Can the following statement be compiled?

         GeometricObject1 object = triangle;

3) Can the following statements be compiled?

  
         GeometricObject1 object = new GeometricObject1();
         Triangle triangle = (Triangle)object;

4) From (3) above, what is the meaning of the following statement?

         Triangle triangle = (Triangle)object;

Back to CS-176/501B Home


cs_176/501b_assignments.txt · Last modified: 2015/03/06 14:55 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki