Site Search

# CS 110: Program 4

## Student Learning Objectives

• Demonstrate the ability to write a Java user-defined class: `Circle`
• Use private instance variables and public methods in your user-defined class
• Write a constructor method that accepts arguments
• Write setter and getter methods
• Use a named constant for the value of PI
• Use objects and method calls in your test program
• Use of jGRASP to type in, compile, and execute a simple Java program
• Include a file header comment to identify the author, account, and honor code
• Browse through the Java Style Guide for other standards and conventions to use in CS 110.

## Introduction

Write a `class` that, given a circle's radius, has methods that return the circle's area, diameter, and circumference.

In case you have forgotten, the equations for a circle's area, diameter, and circumference is given below.

## Test Driven Development

1. Hand-calculate the circle's area, diameter, and circumference based on its radius contained on the CS 110 Program 4 Grading Sheet
• Assume pi is equal to 3.14159
• This hand-calculations are part of your programming score, so do not skip this step
5.3
8.7
2.6

## Program 4 Assignment

Based on Chapter 3, Programming Challenge # 8 `Circle` class in your textbook

• Write a class named `Circle` that has the following fields:
• PI: a `final` double initialized with the value 3.14159
• Note: Use only these two fields, you should not add additional instance variables or constants
• The class should have the following methods:
1. Constructor: Accepts the radius of the circle as an argument
4. getArea: Returns the area of the circle as a double
5. getDiameter: Returns the diameter of the circle as a double
6. getCircumference: Returns the circumference of the circle as a double

Write a separate class called `CircleDemo` with a main method that asks the user for the circle's radius, creates a Circle object, and then reports the circle's area, diameter, and circumference using the circle's getter methods

### Sample Output

Duplicate the format of the input prompt and output exactly. Test your output with different values for the radius.

Hint: the tab character (`\t`) is used for alignment.

Include a file header comment at the top of both source code files. Include the assignment number, your cs110 account number, your name, completion time, and the honor code.

Remember, program assignments are to be done on your own. If you receive help understanding or debugging the assignment, you must acknowledge the person who helped. You must do the coding on your own.

```//**********************************************************
// Assignment: Program 4
// Account: (Enter your cs110 account number here)
//
// Author: (Enter your full name here)
//
// Completion time: (Enter the total number of hours you
//                   spent on the total assignment)
//
// Honor Code: I pledge that this program represents my own
//   program code. I received help from (enter the names of
//   others that helped with the assignment, write no one if
//   you received no help) in understanding and debugging my program.
//*********************************************************
```

Add inline comments for every three to five lines of code. Use an inline comment before every Circle class method explaining what the method does. Skip a blank line before every inline comment.

## Optional Extra Credit

Create a Java Applet saved as `DrawingApplet.java` that draws a picture of something fun. You may use literal numeric values in your method calls, you do not need to use your Rectangle or Circle classes.

Refer to the Java API documentation on the Graphics drawing class to learn how to draw rectangles, ovals, strings, etc.

You may use the skeleton code below to get started on your applet

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class DrawingApplet extends JApplet
{
public void paint(Graphics g)
{
// Set the drawing color to blue and draw a rectangle
g.setColor(Color.decode("#0000FF"));
g.fillRect(20, 30, 50, 60);

// Set the drawing color to red and draw a oval
g.setColor(Color.decode("#FF0000"));
g.fillOval(50, 50, 30, 30);

// Set the drawing color to black and draw a string
g.setColor(Color.decode("#000000"));
g.drawString("Ed Gellenbeck", 20, 110);
}
}

## What to turn in

• The CS 110 Program 4 Grading Sheet (PDF) with the table of three test cases filled in
• Write your full name and account number on the Program 4 Grading Sheet
• Be sure both files are saved in your `U:\programs\program4\` folder on the computer science network.
• Note: if you complete the assignment on your personal computer, you must copy the file(s) to your your CS network account
• An easy way to copy the files is to use a USB portable drive, email, or a digital drop box
• Instructions for accessing your U-Drive remotely is provided if you are unable to physically come into the Hebeler lab to copy the files
• The assignment is due at the start of the lab 8
• Your program will be checked online. You do not need to print out a copy of your program to turn in.
• If you are unable to finish in time, you may turn in what you have done to receive partial credit for the assignment.
• You are allowed to drop one program assignment score for the quarter
• If you decided to try for the optional extra credit Applet, be sure the `DrawingApplet.java` file is saved in your `program4` folder.
• You do not need to turn in anything extra for your DrawingApplet.java program. It will be graded online

20 points possible plus a possible 3 points extra credit -- CS 110 Program 4 Grading Sheet (PDF)

• 8 points - Output Correctness
• The program produces the correct Circle area, diameter, and circumference (-1 per error, 6 points total)
• Duplicate the sample output exactly; do not make up your own words or formatting. (2 points possible)
• 4 points - Class Definition
• The `Circle` class is defined correctly with
• private field for radius (1 point possible)
• private field for the named constant PI (1 point possible)
• methods declared as `public` and implemented correctly (1 point possible)
• The `CircleDemo` class main method uses the `Circle` class methods correctly (1 point possible)
• 3 points - Comments and Alignment
• Skip a blank line before every single-line comment
• Generally one single-line comment is needed for every four to six lines of code
• Use a single-line comment before every method in Circle to explain what the method does
• Use jGRASP Generate CSD to properly align your code
• Alignment and indentation are important. Read through the Java Style Guide for CS 110 expectations
• 2 points - Identifier Names
• Meaningful identifier names used for all identifiers (-1 per problem, 2 points possible)
• Variable and method names should begin with a lower case letter, and each subsequent word should start with an upper-case letter.
• Class names should begin with an upper case letter, and each subsequent word should start with an upper-case letter.
• Named constant names should be in all capital letters and the underscore character used to separate words if necessary
• Avoid the use of abbreviations in choosing variable names
• Read through the Java Style Guide for CS 110 expectations.
• 3 points - Creation of Assignment Folder, File, and Printouts
• Program files `Circle.java` and `CircleDemo.java` created correctly and saved in `U:\programs\program4\` (2 points possible)
• Test cases submitted correctly (1 point possible)
• up to 3 points - Extra Credit
• The picture drawn is fun and creative. Very impressive (+3 points extra credit)
• A fun picture is drawn (+2 points extra credit)
• You drew some different shapes with the Applet (+1 point extra credit)