Site Navigation

Site Search

CS 110: Program 4

Student Learning Objectives

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.

Circle equations

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
RadiusAreaDiameterCircumference
5.3   
8.7   
2.6   

Program 4 Assignment

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

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

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.

File Header Comment

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);  
   }
}

applet screen capture

What to turn in

Grading Criteria

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