Programming Assignments for CS 490A

The programs that you turn in will be graded according to the criteria given in the first of the following three links. The second link gives detailed instructions on exactly how you are to turn in your finished assignments. The programs that you turn in should conform to the style guide contained in the third link.

Program grading criteria
Turning in your assignments
Java Language Coding Guidelines

The style guide is a PDF file; you should print it out and keep it for reference while you are programming. It was written by Cay Horstmann, a well known Java author.

Below are your Java programming assignments. The due date for each one is contained in the assignment description.

Assignment 10.

Do Programming Project 3 on page 481 at the end of Chapter 11. For this assignment you need to create four files, Personnel.java, Employee.java, HourlyEmployee.java, and SalariedEmployee.java. When you are done, put all four of these files into a single zip file with the name CS490Ass10Surname.zip and turn in the zip file. This assignment is due Monday, December 10 (the first day of final exam week).

Assignment 9.

Implement a class Car. A Car object should have two instance variables, one for fuel efficiency (measured in miles per gallon) and one for fuel level (measured in gallons). The fuel efficiency of a car should be specified as a parameter in the Car constructor and the constructor should set the fuel level to zero. There should be getFuelEfficiency, setFuelEfficiency, getFuelLevel, and setFuelLevel methods. There should also be a method addFuel(double gallons) which adds a specified amount to the fuel level, and there should be a method drive(double miles) which simulates driving the car a specified distance. The drive method should adjust the fuel level by the amount of fuel used and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel.

Write a class CS490Ass9Surname that tests your Car class. The test class should prompt a user for a fuel efficiency, construct a Car object, prompt the user for an amount of fuel, put the fuel in the Car object, prompt the user for a distance to travel, drive the appropriate distance, and then report back to the user either the fuel level in the car or the distance actually travelled depending on whether or not there was enough fuel in the car to drive the requested distance. Your test program should let the user drive the car until the user enters a distance of zero. Then your test program should prompt the user for an amount of fuel to add to the car and then let the user drive the car some more. If the user enters zero for the amount of fuel to add, then your test program should prompt the user for a new fuel efficiency and construct a new Car object and let the user drive the new car. Your program should terminate if the user enters zero for the fuel efficiency.

Put your two classes into one file with the name CS490Ass9Surname.java. Put the Car class first. The test class should have a public modifier and the Car class should have no modifier. This assignment is due Monday, November 26.

Assignment 8.
Write a method called printFrame that takes three parameters, two of them (even) integers and the third a string. This method should print a "frame" in the console window consisting of two squares, one inside of the other, with the corners of the squares connected with 45 degree lines. The dimensions of the two squares are given by the two integer parameters and the frame should be drawn using the first character from the input string. Here are a couple of examples. The method call printFrame(20, 12, "=") should produce the following output in the console window. (Notice that these really are "squares" in that they have the same number of characters in their horizontal and vertical sides.)
====================
==                 =
= =              = =
=  =            =  =
=   ============   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   =          =   =
=   ============   =
=  =            =  =
= =              = =
==                ==
====================
The method call printFrame(6, 24, "hello") should produce the following output. Notice that the larger of the two input integers should be the dimension of the outer "square".
hhhhhhhhhhhhhhhhhhhhhhhh
hh                    hh
h h                  h h
h  h                h  h
h   h              h   h
h    h            h    h
h     h          h     h
h      h        h      h
h       h      h       h
h        hhhhhh        h
h        h    h        h
h        h    h        h
h        h    h        h
h        h    h        h
h        hhhhhh        h
h       h      h       h
h      h        h      h
h     h          h     h
h    h            h    h
h   h              h   h
h  h                h  h
h h                  h h
hh                    hh
hhhhhhhhhhhhhhhhhhhhhhhh
Also write a main method that prompts the user for the two even integer dimensions and a string. If one of the input integers is not even, ask the user for another integer. The main method should then call printFrame with the user's parameters. The main method should continue to prompt the user for input and draw "frames" until the user inputs 0 as one of the dimensions. This assignment is due on Monday, November 12.

Assignment 7.
Write a method to convert Celsius to Fahrenheit using the following declaration
private static double cel2Fahr(double cels)
Write another method using the following declaration
private static void printTable(double celsLow, double celsHigh, int n)
that uses a for-loop and the method cel2Fahr to print out a table of temperature conversions, like the ones shown below, with n number of rows in the table and the Celsius temperatures running from celsLow to celsHigh. So, for example, the call
printTable(0, 10, 6)
should produce the following table.
          Cels. Temp.            Fahr. Temp.
          ------------------------------------
          10.0                     50.0
          8.0                     46.4
          6.0                     42.8
          4.0                     39.2
          2.0                     35.6
          0.0                     32.0
And the call printTable(1,3,5) should produce the following table.
          Cels. Temp.           Fahr. Temp.
          -------------------------------------
          3.0                    37.4
          2.5                    36.5
          2.0                    35.6
          1.5                    34.7
          1.0                    33.8
(Do not worry if your columns do not line up perfectly. If you want to, you can use the method from page 265 of the textbook to round your results to one or two decimal places.) Write a program that asks a user to enter values for n, celsLow, and celsHigh and then calls printTable. Your program should continue to prompt the user for n, celsLow, and celsHigh and call printTable until the user inputs zero for n. This assignment is due on Monday, October 29.

Assignment 6.
Do Programming Project 4 on page 251 at the end of Chapter 6 of our textbook. This assignment is due on Monday, October 22.

Assignment 5.
Write a program that creates a 100 element array of integers and fills the array with randomly generated integers between 1 and 100 with the additional requirement that there be no numbers that are repeated in the array (so every number between 1 and 100 will appear exactly once in the array). After filling the array, your program should print out the array as a table with ten rows and ten columns. (You do not need to worry about getting all the columns to line up perfectly in your output.) After your code for printing out the array, place the following code in your program (change the name yourArray to the name of your array). After this block of code, have your array printed out again as a table with ten rows and ten columns.
   for (int i = 1; i < yourArray.length; i++)
   {
     int numberToInsert = yourArray[i];
     int j = i-1;
     while (j >= 0 && numberToInsert < yourArray[j])
     {
       yourArray[j+1] = yourArray[j];
       j--;
     }
     yourArray[j+1] = numberToInsert;
   }
(This block of code will sort your array into ascending order. This makes it easy to see if your array contains any duplicate numbers.) This assignment is due on Monday, October 15.

Assignment 4.
Do Programming Project 7 on page 177 at the end of Chapter 4 of our textbook. Use the method Integer.parseInt to convert the string userInput into an integer (see page 61 of the textbook). This assignment is due on Friday, September 28.

Assignment 3.
Do Programming Project 2 on page 125 at the end of Chapter 3 of our textbook. This assignment is due on Friday, September 21. When you turn in this assignment, you will turn in two classes, your version of the Account class and your version of TestAccount. There are two ways that you can turn in these two classes.
  • One way is for you to put both of your classes in one file. Take your version of TestAccount.java and rename it CS490Ass3Surname.java (and be sure to rename the class from TestAccount to CS490Ass3Surname). Then copy your version of the Account class into this file. Put your version of the Account class after the end of the definition of the CS490Ass3Surname class and remove the public modifier from the definition of the Account class. Then turn in the file CS490Ass3Surname.java in the usual way.
  • The other way is to put your two files Account.java and TestAccount.java into a single zip file. You do this using the WinZip program. Tell WinZip to name the zip file CS490Ass3Surname.zip. Then email the file CS490Ass3Surname.zip as an attachment. If you do not know how to install or use WinZip, then ask me and I will give you a demonstration of how to use it (WinZip is a very useful program and it is worth learning the basics of using it).

Assignment 2.
Do Programming Project 4 on page 81 at the end of Chapter 2 of our textbook. The last sentence of the Programming Project says to round dollar amounts to the nearest cent. You do not have to do this if you do not want to. This assignment is due on Friday, September 14.

Assignment 1.
Do Programming Project 2 on page 81 at the end of Chapter 2 of our textbook. The output of your program should exactly match the picture in the book. Your program will consist of just a sequence of print statements, one for each line of the picture. The main purpose of this assignment is to get you started using the editor and the compiler. This assignment is due on Friday, September 7.

Return to the main Java page.
Return to the CS 490A home page.


compliments and criticisms