CS 100: Lecture L2
January 28
Template for problems with no graphics:
Obtained by editting the file CUCSApplication.java file that is generated when a project is created using CU CS Application stationary.
// Place high-level comments here import java.io.*; public class MyClassName { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); // My contributions begin here...
// and end here. in.waitUntilEnter(); } }
Example L2A: Surface Area Increase
// L2A // Example L2A: Surface Area Increase import java.io.*; public class L2A { public static void main( String args[]){ TokenReader in = new TokenReader(System.in); final double R = 3950.0; // Earth radius (miles). double pavingThickness; // Paving thickness (inches) double deltaR; // Paving thickness (miles) double increase; // Surface area increase (square miles). System.out.println("\nEnter paving thickness (inches): "); pavingThickness = in.readDouble(); deltaR = pavingThickness/(12.0*5280.0); increase = 4.0*Math.PI*(2.0*R+deltaR)*deltaR; System.out.println("Surface area increase = " + increase + " square miles"); Format.println(System.out,"Surface area increase = %10.4e square miles",increase); in.waitUntilEnter(); } }
Template for problems with graphics:
Obtained by editting the file CUCSGraphicsApplication.java file that is generated when a project is created using CU CS Graphics Application stationary.
// High-level comments here import java.io.*; import java.awt.*; public class CUCSDrawing extends Frame { // Place constants here public void paint(Graphics g) { // Your contributions start here.
// And end here. } } public class MyClassName { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(); d.resize(200,150); // Modify as appropriate. d.move(0,75); d.setTitle("Drawing"); // Modify as appropriate. d.show(); d.toFront(); } }
Example L2B: A Sliced Pizza
// Example L2B: Draw a pizza with 8 slices. import java.io.*; import java.awt.*; public class CUCSDrawing extends Frame { final int xc = 450; final int yc = 350; // The circle has center (xc,yc). final int r = 300; // The circle has radius r. public void paint(Graphics g) { // ------------------------------- g.drawOval(xc-r,yc-r,2*r,2*r); // Draw the circle. g.drawLine(xc-r,yc,xc+r,yc); // Draw the horizonal diameter. g.drawLine(xc,yc-r,xc,yc+r); // Draw the vertical diameter. int d; // Declare the "displacement factor". d = (int) (r/Math.sqrt(2.0)); // The 45-degree points have coordinates (in counter-clockwise order) // (xc+d,yc-d), (xc-d,yc-d), (xc-d,yc+d), (xc+d,yc+d) g.drawLine(xc+d,yc-d,xc-d,yc+d); // Connect the first with the third. g.drawLine(xc-d,yc-d,xc+d,yc+d); // Connect the second with the fourth.
// -------------------------------------- } } public class L2B { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(); d.resize(1000,800); d.move(0,75); d.setTitle("S1_4"); d.show(); d.toFront(); } }