CSC 012

Introduction to Computer Science

Summary of 2/25 Lecture

 


By popular demand, I've enhanced the DrawingBox class a bit by adding a drawArc() method that allows you to add arcs to your figures.  The details can be found in the updated DrawingBox documentation.  The following adds an arc to our previous mad mix of circles, squares, ovals, and colors.

arcplay.jpg (12078 bytes)

The code can be found at

//File:  ArcPlay.java
import CSLib.*;
import java.awt.*; //This is where the Color class "lives"
public class ArcPlay
{
    public static void main (String [] args)
    {
        DrawingBox pad = new DrawingBox("Playing Around With Shapes and Colors");
       
        //All the other stuff is in here                        
                       
        pad.drawArc (100, 250, 50, 50, 0, 90);
                        //Draw an arc from 0 to 90 degrees within an imaginary rectangle
                        //whose upper left hand corner is located at (100, 250) and
                        //whose width and height are 50 pixels

    }
}

If you want to upgrade your own DrawingBox class, just click on the following:

DrawingBox.java

and save it to your hard drive in the C:\kamin\CSLib folder.  You must then get an MS-DOS window and navigate to that directory and do the following:

C:\kamin\CSLib>javac DrawingBox.java

Lab Exercise.  Using the new enhanced DrawingBox, draw a smiley face.


Let's revisit the lab exercise.  We can allow more flexibility in the use of our work if we replace the main() method in Olympic.java with a more generic, say, drawIt() as follows:

//File:    Olympic.java
import java.awt.*;
import CSLib.*;
public class Olympic
{
    public static void drawIt()
    {
        DrawingBox pad = new DrawingBox ("Olympic Logo");
        pad.setSize (330, 225);
       
        pad.setColor (Color.BLUE);
        pad.drawCircle (50, 50, 50);
       
        pad.setColor (Color.BLACK);
        pad.drawCircle (160, 50, 50);
       
        pad.setColor (Color.RED);
        pad.drawCircle (270, 50, 50);
       
        pad.setColor (Color.YELLOW);
        pad.drawCircle (105, 100, 50);
       
        pad.setColor (Color.GREEN);
        pad.drawCircle (215, 100, 50);
       
        pad.setColor (Color.BLACK);
        pad.drawString ("Olympic Logo", 125, 180);
    }
}

We can then write a client or driver program that incorporates our logo as desired.

//File:    OlympicClient.java
public class OlympicClient
{
    public static void main (String [] args)
    {
        Olympic logo = new Olympic();
       
        logo.drawIt();
    }
}

olympic.jpg (9355 bytes)

Exercise.  Write a class to draw 3 concentric circles.   The following does just that.  After seeing how the drawOval() method works, we accomplished the same thing using the more obvious drawCircle() method.  You have to be careful though.  The parameters for the drawOval() method involve the coordinates of the upper left hand corner of the enclosing rectangle, while the drawCircle() method uses the coordinates of the center of the circle.

//File:    Concentric.java
import CSLib.*;

public class Concentric
{
    // Draw concentric circles.
    // Author: Adrienne Baranowicz, December 2, 2000

    public void drawThem ()
    {
        int coord;
        int diam;
        DrawingBox g = new DrawingBox();
       
        g.setDrawableSize(300, 300);
       
        coord = 110; diam = 80;
        g.drawOval(coord, coord, diam, diam);
        //ALTERNATIVE:
        //g.drawCircle(150, 150, diam/2);
       
        coord = 95; diam = 110;
        g.drawOval(coord, coord, diam, diam);
        //ALTERNATIVE:
        //g.drawCircle(150, 150, diam/2);
       
       
        coord = 80; diam = 140;
        g.drawOval(coord, coord, diam, diam);
        //ALTERNATIVE:
        //g.drawCircle(150, 150, diam/2);
    }
}

//File:    ConcentricClient.java
import CSLib.*;

public class ConcentricClient
{
    public static void main (String[] args)
    {
        Concentric circles;
        circles = new Concentric();
        circles.drawThem();
    }
}

circles.jpg (9607 bytes)

To make things a bit more interactive, we resumed the use of the InputBox Class.  This allows the user to determine the outcome of the program execution.

Exercise.  Write a class that allows the user to determine the size (diameter) and then generates the appropriate circle.

//File:    DrawCircle.java
import CSLib.*;

public class DrawCircle
{
    // Draw circle.
    // Author: Sam Kamin, April 10, 2001

    public void draw ()
    {
        DrawingBox g;
        g = new DrawingBox();
        g.setDrawableSize(300, 300);

        InputBox in;
        in = new InputBox();
        in.setPrompt("Enter diameter (<= 300): ");
       
        int diam;
        diam = in.readInt();

        g.drawOval(150-diam/2, 150-diam/2, diam, diam);
    }
}

//File:    DrawCircleClient.java
public class DrawCircleClient
{
    public static void main (String [] args)
    {
        DrawCircle circle = new DrawCircle();
       
        circle.draw();
    }
}

wpe39.jpg (4663 bytes)    wpe3A.jpg (8413 bytes)

 

The Assignment Statement

With the following exercise, we began the study of the assignment statement.  We discovered that the = sign does not mean equality, but rather assignment.  The value on the right hand side of the = is stored in the memory location referenced on the left hand side.  The following class is commented to enlighten further.

Exercise.  Write a class that allows the user to submit two integers and then echo them to a DrawingBox object.

//File:    EchoTwoIntegers.java
import CSLib.*;

public class EchoTwoIntegers
{

    // EchoTwoIntegers: read two integers and print in reverse
    // Author: S. Kamin, April 13, 2001

    void echo ()
    {
        InputBox in;
        OutputBox out;
        int i;
        int j;

        i = 2;    //Assigns the value 2 to the memory location labeled i
        j = i;    //Assigns the value residing in memory location i to the memory location labeled j
        j = j + 1;    //Adds 1 to the value residing in memory location j and stores the result in
                //memory location labeled j
       
        //lhs = rhs;    The right hand side (rhs) is evaluated
        //                  The result is stored in the memory location referenced by left hand side (lhs)
        //2 = j;          ILLEGAL ==>     2 cannot reference a memory location since identifiers must begin with
        //                                               a letter, not a number
       
        in = new InputBox();      //construct an InputBox and call it "in"
        in.setPrompt("Enter an integer:");
        i = in.readInt();
        in.setPrompt("Enter another integer:");
        j = in.readInt();
       
        out = new OutputBox();      //Construct an OutputBox and call it out
        out.print("The integers you entered were ");
        out.print(j);
        out.print(" and ");
        out.print(i);
        out.println(".");
    }
}

//File:    EchoClient.java
import CSLib.*;

public class EchoClient
{
    public static void main (String[] args)
    {
        EchoTwoIntegers ec = new EchoTwoIntegers();
        ec.echo();
    }
}

wpe3B.jpg (4294 bytes)    wpe3C.jpg (4447 bytes)     wpe3D.jpg (15650 bytes)

The following example introduces the other number type, the double.   These numbers allow decimals and fractions.

Exercise.  Write a class that converts a temperature in degrees Fahrenheit to a corresponding temperature in degrees Celcius (or Centigrade).

//File:    Temperature.java
import CSLib.*;

public class Temperature
{
    // Convert temperature from Fahrenheit to Centigrade
    // Author: Samuel N. Kamin, June 1, l996

    public void compute ()
    {
        double temperature;      // The Fahrenheit temperature.
        InputBox in;
        OutputBox out;

        in = new InputBox();
        in.setPrompt ("Please type the temperature (deg F): ");
        temperature = in.readDouble();

        out = new OutputBox();
        out.print(temperature);
        out.print(" deg F is ");
        out.print(5.0 * (temperature - 32.0) / 9.0);
        out.println(" deg C");
    }
}

//File:    TemperatureClient.java
public class TemperatureClient
{
    public static void main (String[] args)
    {
        Temperature test = new Temperature();
        test.compute();
    }
}

wpe3E.jpg (5567 bytes)    wpe3F.jpg (15319 bytes)

Back to CSC 012 Home Page