CS 100: Section Assignment S6
Solutions
// Illustrates strings, methods that return a string, fonts, color,
// a graphics methods.
import java.io.*;
import java.awt.*;
public class S6_1Drawing extends Frame
{
// Yields a time string of the form h:mm or hh:mm
public static String prettyTime(int hour, int minute)
{
String s;
if (minute<10)
{
s = hour + ":0" + minute;
}
else
{
s = hour + ":" + minute;
}
return s;
}
public static void drawDigitalClock(Graphics g, int hc, int vc, int r, int hour, int minute)
{
// Draw the face and the rectangles
g.setColor(Color.magenta);
g.fillOval(hc-r,vc-r,2*r,2*r);
g.setColor(Color.pink);
g.fillRect(hc-r/2,(int)(vc-.45*r-r/6),r,r/3);
g.fillRect(hc-r/2,(int)(vc+.45*r-r/6),r,r/3);
// Draw the labels
g.setColor(Color.black);
g.setFont(new Font("Helvetica",Font.PLAIN,18));
g.drawString("Time",(int)(hc-.1*r),(int)(vc-.15*r));
g.drawString("Coming Up",(int)(hc-.25*r),(int)(vc+.75*r));
// Produce the two time strings
String current = prettyTime(hour,minute);
int m1 = minute+1;
int h1 = hour;
if (m1==60)
{
m1 = 0;
h1 = h1+1;
if (h1==13)
{
h1 = 1;
}
}
String comingUp = prettyTime(h1,m1);
// Display the two times.
g.setFont(new Font("TimesRoman",Font.BOLD,36));
g.drawString(current,(int)(hc-.2*r),(int)(vc-.38*r));
g.drawString(comingUp,(int)(hc-.2*r),(int)(vc+.53*r));
}
// Whenever it has to the system will execute this method and
// draw the specified clock in the window.
public void paint(Graphics g)
{
drawDigitalClock(g,400,300,200,12,59);
}
}
public class S6_1
{
public static void main(String args[])
{
// Create a window for the system to draw in.
S6_1Drawing d = new S6_1Drawing();
d.resize(800,600);
d.move(0,75);
d.setTitle("Digital Watch");
d.show();
d.toFront();
}
}
//*********************************************************
// Illustrates how to write a graphics method and some "alternation"
// type thinking.
import java.io.*;
import java.awt.*;
public class S6_2Drawing extends Frame
{
// Graphics g draws a row of an n-by-n checkerboard with s-by-s squares.
// The upper left corner of the first square is at (hL,vT). The squares alternate
// in color between magent and cyan. The leftmost square magenta if first is true
// and cyan otherwise.
public static void drawCheckerRow(Graphics g, int hL, int vT, int s, int n, boolean first)
{
int h = hL; // The horizontal coordinate of the kth square's left edge.
for(int k=1;k<=n;k++)
{
// The kth square is magent if k is odd and the first square is magenta OR
// if k is even and the first square is cyan.
if((k%2==1 && first) || (k%2==0 && !first) )
g.setColor(Color.magenta);
else
g.setColor(Color.cyan);
g.fillRect(h,vT,s,s);
h=h+s;
}
}
public void paint(Graphics g) {
drawCheckerRow(g,100,200,50,12,true);
drawCheckerRow(g,100,250,50,12,false);
}
}
public class S6_2
{
public static void main(String args[])
{
S6_2Drawing d = new S6_2Drawing();
d.resize(800,600);
d.move(0,75);
d.setTitle("Checker Row");
d.show();
d.toFront();
}
}
//************************************************
// Nested if-else and string equality.
import java.io.*;
public class S6_3
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
String s = "Autumn";
//String s = "Winter";
//String s = "Spring";
//String s = "Summer";
String date;
if (s.equals("Autumn"))
date = "September 22";
else if (s.equals("Winter"))
date = "December 21";
else if (s.equals("Spring"))
date = "March 22";
else
date = "June 22";
System.out.println(s + " begins on " + date);
in.waitUntilEnter();
}
}
//******************************************************
// Illustrate nested if-else.
import java.io.*;
public class S6_4
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
String s; // Names the quadrant(s)
int x; // A trial degree
for(x=0;x<=360;x=x+45)
{
// There are 8 separate possibilities: 4 boundary situations
// a 4 nonboundary situations.
if ( (0<x) && (x<90) )
s = "First";
else if ( (90<x) && (x<180) )
s = "Second";
else if ( (180<x) && (x<270) )
s = "Third";
else if ( (270<x) && (x<360) )
s = "Fourth";
else if ( x==0 || x==360 )
s = "First and Fourth";
else if (x==90)
s = "First and Second";
else if (x==180)
s = "Second and Third";
else
s = "Third and Fourth";
System.out.println(x + " is in the " + s + " quadrant(s)");
}
in.waitUntilEnter();
}