Homework 2 Sun., 9/12 Due Mon., 9/13 Problems available on-line ( MT sections ) Due at 11:59 pm Mon., 9/13 ( WTh sections ) leeway ? Problems available on-line http://www.cs.hmc.edu/courses/2004/fall/cs5/week_02/homework.html CS 5 website http://www.cs.hmc.edu/courses/2004/fall/cs5/
Tutors available -- contact information
Tutors available -- contact information Available in Parsons PC Labs (the CIS labs) Lab Code: 3-1-2-5 Blg Code: 0014416 Available in the LAC Lab (Linde Activities Center) Blg Code: 1234112 Starting next week... You may also seek out tutors away from the labs, by phone, in the dorms, etc.
Steps for handling HW problems Download a new HW zip file Unzip it (easiest way: right-click then choose Winzip -> Extract to here) Read the problem (!) Write the new program Test it thoroughly Double-click the CS5hw.jcw file to start JCreator CS5hw.jcw Submit only CS5App.java -- it will have this icon on the PC. This is your source code! Submit the CS5App.java file in the source_code folder Check to be sure that your code appears after submitting. [optional] Download and replace/copy into CS5App.java elsewhere Resubmit anytime up to the deadline.
Problem 1 Hello, World! program /* * CS5App.java * * Homework # 2 * Name: me! * Date: 9/10/2004 * Time spent: 18 hours * Comments: * Phew… */ import java.awt.*; class CS5App { public static void main(String[] args) H.pl("Go Warts!"); } Hello, World! program Alternatives are also welcome...
Problem 1 insane Hello, World! program /* * intro things here... */ class CS5App { public static void main(String[] args) H.pl(h + e + l + l + o + w + o + r + l + d); }
Problem 1 insane Hello, World! program /* * intro things here... */ class CS5App { public static void main(String[] args) String h = “H”, e = “e”; String l = “l”, o = “o”; String w = “ W”, r = “r”; String d = “d!”; H.pl(h + e + l + l + o + w + o + r + l + d); } not that you would… but you could!
Abstract(ion) Art Problem 2 art.add(GrRectangle(4,6,6,3,Color.???)); width height upper-left y coordinate upper-left x coordinate my foreign language teachers always tried to convince me that the skill of speaking, reading and writing a foreign language… I didn’t believe them then and I don’t believe them now -- but I do believe that reading and writing a computer language is the way to get a strong intuitive understanding of how machines think -- it’s also important for getting them to do what you want them to, but even if you forget everything this course (hopefully) teaches about Java (compiler, datatypes, Classes and Objects), the real goal is that you take away a deeper insight into how machines think Very first assignment -- ask you to use your ability to handle abstractions a lot! Hello, World program -- lots of stuff Painting program -- I’d be surprised if anyone hadn’t used a program to create or modify images very limited version -- but one that exposes the underlying language -- or thought -- that all of the painting/Photoshop programs share What baggage comes with graphical programming -- even this simple example relies on it art.add(GrRectangle(4,6,6,3,Color.???)); Problem 2
Problem 2 Create the following “work” art.add(GrRectangle( ); In as few drawing commands as possible... 6 rectangles are the minimum my foreign language teachers always tried to convince me that the skill of speaking, reading and writing a foreign language… I didn’t believe them then and I don’t believe them now -- but I do believe that reading and writing a computer language is the way to get a strong intuitive understanding of how machines think -- it’s also important for getting them to do what you want them to, but even if you forget everything this course (hopefully) teaches about Java (compiler, datatypes, Classes and Objects), the real goal is that you take away a deeper insight into how machines think Very first assignment -- ask you to use your ability to handle abstractions a lot! Hello, World program -- lots of stuff Painting program -- I’d be surprised if anyone hadn’t used a program to create or modify images very limited version -- but one that exposes the underlying language -- or thought -- that all of the painting/Photoshop programs share What baggage comes with graphical programming -- even this simple example relies on it What command would draw this blue rectangle? art.add(GrRectangle( );
Problem 3 Printing exactly this text: /-------------------------------------\ | Welcome to the frugal arithmetician | \-------------------------------------/ The goal is to print several numbers using two variables: an integer, x, which equals 5 a double, d, which equals 42.0 along with the operators +, -, *, /, %, (int), and (double) . Five is 5, and ten is 10. "one" is 1 "three" is 3 you're "young" til 36 "one-third" is 0.33 "ten thousand" is 10000 "pi" is 3.14159 /-------\ | Bye ! | \-------/
Hw2Pr3) A printing puzzle... 10000 H.p(x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x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+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x); Extra Credit: In as few operations as possible!
be sure not to use only combinations of d and x for these numbers Printing exactly this text: Problem 3 /-------------------------------------\ | Welcome to the frugal arithmetician | \-------------------------------------/ The goal is to print several numbers using two variables: an integer, x, which equals 5 a double, d, which equals 42.0 along with the operators +, -, *, /, %, (int), and (double) . Five is 5, and ten is 10. "one" is 1 "three" is 3 you're "young" til 36 "one-third" is 0.33 "ten thousand" is 10000 "pi" is 3.14159 /-------\ | Bye ! | \-------/ 37 hyphens here… 11 spaces here… be sure not to use only combinations of d and x for these numbers
Problem 3 Put the variables at the top… Lots more printing here /* * Be sure to fill in this comment... */ class CS5App { public static void main(String[] args) int x = 5; double d = 42.0; H.pl(“/-------------------------------------\”); H.pl(“| Welcome to the frugal arithmetician |”); } Put the variables at the top… Lots more printing here remember: \
No other numbers! you’re "young" til 36 How do we print this? int x = 5; double d = 42.0; you’re "young" til 36 How do we print this? "pi" is 3.14159 or this ?!?
parentheses are important ! int x = 5; Variable types double d = 42.0; and many, many more… H.pl(“x+d is ” + x + d); String + int creates a String “x+d is 5” String + double creates a String “x+d is 542.0” parentheses are important !
Maybe a little less precision… Java’s default is maximum precision: double d = 5/3.0; H.pl( d ); 1.6666666666666667 H has some built-in formatting commands: H.pl( H.fmt(d) ); 1.667 H.pl( H.fmt(d,4) ); 1.6667
What Java is thinking *%*/?! double d = 5/3.0; H.fmt(d,4); H.pl( “five-thirds is ” + d ); five-thirds is 1.6666666666666667 the only way to change a variable is with the = operator! but we don’t want to change d, we only want to change the String that’s printed! H.pl( “five-thirds is ” + H.fmt(d,4) ); five-thirds is 1.6667
H reference Output Input H.pl(x) prints x followed by a newline. H.pl() prints just a newline. H.p(x) prints just x, with no newline following H.fmt(s) returns a String for storing or printing: 3 places after the decimal point H.fmt(s,p) returns a String for storing or printing: p places after the decimal point H.fmt(s,p,w) returns a String for storing or printing: w is the minimum width of the output String H.fmt(s,p,w,HMCOutput.RIGHT) same as above, but puts the number to the right of the output String. Also available: HMCOutput.CENTER and LEFT. Input H.ni() returns the next integer the user types or has typed. H.nw() returns the next word the user types or has typed as a String. H.nd() returns the next double the user types or has typed. H.nl() returns the next line of text the user types as a String. H.nc() returns the next char of text the user types as a char. H.nanyc() returns the next char, even if it’s whitespace. Good for pauses: “Hit Enter to continue...” This list (and more) available from http://www.cs.hmc.edu/courses/2004/fall/cs5/HMCSupport.html
Problem 4 AI program include at least 3 questions... /* * Be sure to fill in this comment... */ class CS5App { public static void main(String[] args) H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); H.nl(); // gets input and ignores it H.pl("Hasta la vista!"); } include at least 3 questions...
Problem 4 -- putting the I in AI public static void main(String[] args) { H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); String name = H.nl(); H.pl("Hasta la vista, " + name + "!"); H.pl("What’s your favorite movie?”); H.pl(name + "is ok, but I liked T2"); } Why will Java complain about this code?
Problem 4 – mixing it up... public static void main(String[] args) { H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); String name = H.nl(); // gets line H.pl("Hi " + name + " I’m a T1000”); H.pl("What’s your model number?"); int modelnum = H.ni(); // gets integer H.pl(“A ” + modelnum + “?”); H.pl(“I thought those were extinct!”); }
Style Matters... you can’t type return inside a Java String... H.pl(“Aha! I’ve tricked you. ” + favDish + “ can’t be your favorite\n” + “ Platt food, because Platt food\n” + “ does not exist!”); H.p(“Aha! I’ve tricked you. ”); H.pl(favDish + “ can’t be your favorite”); H.pl(“ Platt food, because Platt food”); H.pl(“ does not exist!”); handling lots of text 1 keep lines to less than 80 chars long! or 2 be sure you consistently indent within code blocks 3 be sure you line up matching punctuation (curly braces) 4 be sure to have a complete start-of-file comment use 1-line comments to explain complicated code
Submitting Go to Click on http://www.cs.hmc.edu/courses/2004/fall/cs5/ Click on Submit an assignment… Fill in username and your password (also your username, unless you’ve changed it) Choose Homework #2 and the appropriate problem number Click on “Browse” and locate your CS5App.java file It is inside the folder! source_code Be sure NOT to submit the CS5hw file … Problems submitting ? Email me at dodds@cs.hmc.edu
Homework 2 Sun., 9/12 Due Mon., 9/13 Problems available on-line ( MT sections ) Due at 11:59 pm Mon., 9/13 ( WTh sections ) leeway ? Problems available on-line http://www.cs.hmc.edu/courses/2004/fall/cs5/week_02/homework.html CS 5 website http://www.cs.hmc.edu/courses/2004/fall/cs5/
Tutors available -- contact information Available in Parsons PC Labs (the CIS labs) Lab Code: 3-1-2-5 Blg Code: 0014416 Available in the LAC Lab (Linde Activities Center) Blg Code: 1234112 Starting next week... You may also seek out tutors away from the labs, by phone, in the dorms, etc.
Steps for handling HW problems Download a new HW zip file Unzip it (easiest way: right-click then choose Winzip -> Extract to here) Read the problem (!) Write the new program Test it thoroughly Double-click the CS5hw.jcw file to start JCreator CS5hw.jcw Submit only CS5App.java -- it will have this icon on the PC. This is your source code! Submit the CS5App.java file in the source_code folder Check to be sure that your code appears after submitting. [optional] Download and replace/copy into CS5App.java elsewhere Resubmit anytime up to the deadline.
Problem 1 Hello, World! program /* * CS5App.java * * Homework # 2 * Name: me! * Date: 9/10/2004 * Time spent: 18 hours * Comments: * Phew… */ import java.awt.*; class CS5App { public static void main(String[] args) H.pl("Go Warts!"); } Hello, World! program Alternatives are also welcome...
Problem 1 insane Hello, World! program /* * intro things here... */ class CS5App { public static void main(String[] args) H.pl(h + e + l + l + o + w + o + r + l + d); }
Abstract(ion) Art Problem 2 art.add(GrRectangle(4,6,6,3,Color.???)); width height upper-left y coordinate upper-left x coordinate my foreign language teachers always tried to convince me that the skill of speaking, reading and writing a foreign language… I didn’t believe them then and I don’t believe them now -- but I do believe that reading and writing a computer language is the way to get a strong intuitive understanding of how machines think -- it’s also important for getting them to do what you want them to, but even if you forget everything this course (hopefully) teaches about Java (compiler, datatypes, Classes and Objects), the real goal is that you take away a deeper insight into how machines think Very first assignment -- ask you to use your ability to handle abstractions a lot! Hello, World program -- lots of stuff Painting program -- I’d be surprised if anyone hadn’t used a program to create or modify images very limited version -- but one that exposes the underlying language -- or thought -- that all of the painting/Photoshop programs share What baggage comes with graphical programming -- even this simple example relies on it art.add(GrRectangle(4,6,6,3,Color.???)); Problem 2
Problem 2 Create the following “work” art.add(GrRectangle( ); In as few drawing commands as possible... 6 rectangles are the minimum my foreign language teachers always tried to convince me that the skill of speaking, reading and writing a foreign language… I didn’t believe them then and I don’t believe them now -- but I do believe that reading and writing a computer language is the way to get a strong intuitive understanding of how machines think -- it’s also important for getting them to do what you want them to, but even if you forget everything this course (hopefully) teaches about Java (compiler, datatypes, Classes and Objects), the real goal is that you take away a deeper insight into how machines think Very first assignment -- ask you to use your ability to handle abstractions a lot! Hello, World program -- lots of stuff Painting program -- I’d be surprised if anyone hadn’t used a program to create or modify images very limited version -- but one that exposes the underlying language -- or thought -- that all of the painting/Photoshop programs share What baggage comes with graphical programming -- even this simple example relies on it What command would draw this blue rectangle? art.add(GrRectangle( );
be sure not to use only combinations of d and x for these numbers Printing exactly this text: Problem 3 /-------------------------------------\ | Welcome to the frugal arithmetician | \-------------------------------------/ The goal is to print several numbers using two variables: an integer, x, which equals 5 a double, d, which equals 42.0 along with the operators +, -, *, /, %, (int), and (double) . Five is 5, and ten is 10. "one" is 1 "three" is 3 you're "young" til 36 "one-third" is 0.33 "ten thousand" is 10000 "pi" is 3.14159 /-------\ | Bye ! | \-------/ 37 hyphens here… 11 spaces here… be sure not to use only combinations of d and x for these numbers
Problem 3 Put the variables at the top… Lots more printing here /* * Be sure to fill in this comment... */ class CS5App { public static void main(String[] args) int x = 5; double d = 42.0; H.pl(“/-------------------------------------\”); H.pl(“| Welcome to the frugal arithmetician |”); } Put the variables at the top… Lots more printing here remember: \
No other numbers! you’re "young" til 36 How do we print this? int x = 5; double d = 42.0; you’re "young" til 36 How do we print this? "pi" is 3.14159 or this ?!?
parentheses are important ! int x = 5; Variable types double d = 42.0; and many, many more… H.pl(“x+d is ” + x + d); String + int creates a String “x+d is 5” String + double creates a String “x+d is 542.0” parentheses are important !
Formatting doubles Java’s default is maximum precision. double d = 5/3.0; H.pl( d ); 1.6666666666666667 H does have some built-in formatting commands: H.pl( H.fmt(d) ); 1.667 H.pl( H.fmt(d,4) ); 1.6667
What Java is thinking *%*/?! double d = 5/3.0; H.fmt(d,4); H.pl( “five-thirds is ” + d ); five-thirds is 1.6666666666666667 the only way to change a variable is with the = operator! but we don’t want to change d, we only want to change the String that’s printed! H.pl( “five-thirds is ” + H.fmt(d,4) ); five-thirds is 1.6667
H reference Output Input H.pl(x) prints x followed by a newline. H.pl() prints just a newline. H.p(x) prints just x, with no newline following H.fmt(s) returns a String for storing or printing: 3 places after the decimal point H.fmt(s,p) returns a String for storing or printing: p places after the decimal point H.fmt(s,p,w) returns a String for storing or printing: w is the minimum width of the output String H.fmt(s,p,w,HMCOutput.RIGHT) same as above, but puts the number to the right of the output String. Also available: HMCOutput.CENTER and LEFT. Input H.ni() returns the next integer the user types or has typed. H.nw() returns the next word the user types or has typed as a String. H.nd() returns the next double the user types or has typed. H.nl() returns the next line of text the user types as a String. H.nc() returns the next char of text the user types as a char. H.nanyc() returns the next char, even if it’s whitespace. Good for pauses: “Hit Enter to continue...” This list (and more) available from http://www.cs.hmc.edu/courses/2004/fall/cs5/HMCSupport.html
Problem 4 AI program include at least 3 questions... /* * Be sure to fill in this comment... */ class CS5App { public static void main(String[] args) H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); H.nl(); // gets input and ignores it H.pl("Hasta la vista!"); } include at least 3 questions...
Problem 4 -- putting the I in AI public static void main(String[] args) { H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); String name = H.nl(); H.pl("Hasta la vista, " + name + "!"); H.pl("What’s your favorite movie?”); H.pl(name + "is ok, but I liked T2"); } Why will Java complain about this code?
Problem 4 – mixing it up... public static void main(String[] args) { H.pl("Hello, I’m Skynet."); H.pl("What’s your name?"); String name = H.nl(); // gets line H.pl("Hi " + name + " I’m a T1000”); H.pl("What’s your model number?"); int modelnum = H.ni(); // gets integer H.pl(“A ” + modelnum + “?”); H.pl(“I thought those were extinct!”); }
Style Matters... you can’t type return inside a Java String... H.pl(“Aha! I’ve tricked you. ” + favDish + “ can’t be your favorite\n” + “ Platt food, because Platt food\n” + “ does not exist!”); H.p(“Aha! I’ve tricked you. ”); H.pl(favDish + “ can’t be your favorite”); H.pl(“ Platt food, because Platt food”); H.pl(“ does not exist!”); handling lots of text 1 keep lines to less than 80 chars long! or 2 be sure you consistently indent within code blocks 3 be sure you line up matching punctuation (curly braces) 4 be sure to have a complete start-of-file comment use 1-line comments to explain complicated code
Submitting Go to Click on http://www.cs.hmc.edu/courses/2004/fall/cs5/ Click on Submit an assignment… Fill in username and your password (also your username, unless you’ve changed it) Choose Homework #2 and the appropriate problem number Click on “Browse” and locate your CS5App.java file It is inside the folder! source_code Be sure NOT to submit the CS5hw file … Problems submitting ? Email me at dodds@cs.hmc.edu