Homework 2 Due Sun 912 MT sections Mon

  • Slides: 42
Download presentation
Homework 2 • Due Sun. , 9/12 ( MT sections ) Mon. , 9/13

Homework 2 • Due Sun. , 9/12 ( MT sections ) Mon. , 9/13 ( WTh sections ) at 11: 59 pm • Problems available on-line http: //www. cs. hmc. edu/courses/2004/fall/cs 5/week_02/homework. html • CS 5 website http: //www. cs. hmc. edu/courses/2004/fall/cs 5/ leeway ?

 • Tutors available -- contact information

• Tutors available -- contact information

 • Tutors available -- contact information Available in Parsons PC Labs (the CIS

• 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

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 CS 5 hw. jcw Double-click the CS 5 hw. jcw file to start JCreator Submit only CS 5 App. java -- it will have this icon on the PC. This is your source code! • Submit the CS 5 App. java file in the source_code folder • Check to be sure that your code appears after submitting. [optional] • Download and replace/copy into CS 5 App. java elsewhere • Resubmit anytime up to the deadline.

/* * CS 5 App. java * * Homework # 2 * Problem #

/* * CS 5 App. java * * Homework # 2 * Problem # 1 * * Name: me! * Date: 9/10/2004 * Time spent: 18 hours * * Comments: * * Phew… */ Problem 1 • Hello, World! program import java. awt. *; class CS 5 App { public static void main(String[] args) { H. pl("Go Warts!"); } } Alternatives are also welcome. . .

Problem 1 • insane Hello, World! program /* * intro things here. . .

Problem 1 • insane Hello, World! program /* * intro things here. . . */ class CS 5 App { public static void main(String[] args) { H. pl(h + e + l + o + w + o + r + l + d); } }

Problem 1 • insane Hello, World! program /* * intro things here. . .

Problem 1 • insane Hello, World! program /* * intro things here. . . */ class CS 5 App { 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 + o + w + o + r + l + d); } } not that you would… but you could!

Abstract(ion) Art width height upper-left y coordinate upper-left x coordinate art. add(Gr. Rectangle(4, 6,

Abstract(ion) Art width height upper-left y coordinate upper-left x coordinate art. add(Gr. Rectangle(4, 6, 6, 3, Color. ? ? ? )); Problem 2

Problem 2 Create the following “work” In as few drawing commands as possible. .

Problem 2 Create the following “work” In as few drawing commands as possible. . . 6 rectangles are the minimum What command would draw this blue rectangle? art. add(Gr. Rectangle( );

Printing exactly this text: Problem 3 /------------------- | Welcome to the frugal arithmetician |

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 ! | -------/

Hw 2 Pr 3) 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+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+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); Extra Credit: In as few operations as possible!

Printing exactly this text: /------------------- | Welcome to the frugal arithmetician | -------------------/ Problem

Printing exactly this text: /------------------- | Welcome to the frugal arithmetician | -------------------/ Problem 3 37 hyphens here… 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. 11 spaces here… "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 ! | -------/ be sure not to use only combinations of d and x for these numbers

Problem 3 /* * Be sure to fill in this comment. . . */

Problem 3 /* * Be sure to fill in this comment. . . */ class CS 5 App { public static void main(String[] args) { int x = 5; Put the variables double d = 42. 0; at the top… H. pl(“/-------------------”); H. pl(“| Welcome to the frugal arithmetician |”); Lots more printing here } } remember:

int x = 5; double d = 42. 0; No other numbers! you’re "young"

int x = 5; double d = 42. 0; No other numbers! you’re "young" til 36 "pi" How do we print this? is 3. 14159 or this ? !?

Variable types int x = 5; double d = 42. 0; and many, many

Variable types int x = 5; 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.

Maybe a little less precision… Java’s default is maximum precision: double d = 5/3. 0; H. pl( d ); 1. 666666667 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);

What Java is thinking *%*/? ! double d = 5/3. 0; H. fmt(d, 4); H. pl( “five-thirds is ” + d ); five-thirds is 1. 666666667 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 H. pl(x) H. pl() H. p(x) prints x followed by a

H reference Output H. pl(x) H. pl() H. p(x) prints x followed by a newline. 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() H. nw() H. nd() H. nl() H. nc() H. nanyc() returns the next integer the user types or has typed. returns the next word the user types or has typed as a String. returns the next double the user types or has typed. returns the next line of text the user types as a String. returns the next char of text the user types as a char. 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/cs 5/HMCSupport. html

Problem 4 • AI program include at least 3 questions. . . /* *

Problem 4 • AI program include at least 3 questions. . . /* * Be sure to fill in this comment. . . */ class CS 5 App { 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!"); } }

Problem 4 -- putting the I in AI public static void main(String[] args) {

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? ”); String name = H. nl(); H. pl(name + "is ok, but I liked T 2"); } Why will Java complain about this code?

Problem 4 – mixing it up. . . public static void main(String[] args) {

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 T 1000”); H. pl("What’s your model number? "); int modelnum = H. ni(); // gets integer H. pl(“A ” + modelnum + “? ”); H. pl(“I thought those were extinct!”); }

you can’t type return inside a Java String. . . Style Matters. . .

you can’t type return inside a Java String. . . Style Matters. . . handling lots of text 1 keep lines to less than 80 chars long! or 2 3 4 H. pl(“Aha! I’ve tricked you. ” + fav. Dish + “ can’t be your favoriten” + “ Platt food, because Platt foodn” + “ does not exist!”); H. p(“Aha! I’ve tricked you. ”); H. pl(fav. Dish + “ can’t be your favorite”); H. pl(“ Platt food, because Platt food”); H. pl(“ does not exist!”); • be sure you consistently indent within code blocks • be sure you line up matching punctuation (curly braces) • 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/cs 5/ Submit

Submitting • Go to • Click on http: //www. cs. hmc. edu/courses/2004/fall/cs 5/ 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 CS 5 App. java file It is inside the source_code folder! • Be sure NOT to submit the CS 5 hw file … • Problems submitting ? Email me at dodds@cs. hmc. edu

Homework 2 • Due Sun. , 9/12 ( MT sections ) Mon. , 9/13

Homework 2 • Due Sun. , 9/12 ( MT sections ) Mon. , 9/13 ( WTh sections ) at 11: 59 pm • Problems available on-line http: //www. cs. hmc. edu/courses/2004/fall/cs 5/week_02/homework. html • CS 5 website http: //www. cs. hmc. edu/courses/2004/fall/cs 5/ leeway ?

 • Tutors available -- contact information Available in Parsons PC Labs (the CIS

• 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

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 CS 5 hw. jcw Double-click the CS 5 hw. jcw file to start JCreator Submit only CS 5 App. java -- it will have this icon on the PC. This is your source code! • Submit the CS 5 App. java file in the source_code folder • Check to be sure that your code appears after submitting. [optional] • Download and replace/copy into CS 5 App. java elsewhere • Resubmit anytime up to the deadline.

/* * CS 5 App. java * * Homework # 2 * Problem #

/* * CS 5 App. java * * Homework # 2 * Problem # 1 * * Name: me! * Date: 9/10/2004 * Time spent: 18 hours * * Comments: * * Phew… */ Problem 1 • Hello, World! program import java. awt. *; class CS 5 App { public static void main(String[] args) { H. pl("Go Warts!"); } } Alternatives are also welcome. . .

Problem 1 • insane Hello, World! program /* * intro things here. . .

Problem 1 • insane Hello, World! program /* * intro things here. . . */ class CS 5 App { public static void main(String[] args) { H. pl(h + e + l + o + w + o + r + l + d); } }

Abstract(ion) Art width height upper-left y coordinate upper-left x coordinate art. add(Gr. Rectangle(4, 6,

Abstract(ion) Art width height upper-left y coordinate upper-left x coordinate art. add(Gr. Rectangle(4, 6, 6, 3, Color. ? ? ? )); Problem 2

Problem 2 Create the following “work” In as few drawing commands as possible. .

Problem 2 Create the following “work” In as few drawing commands as possible. . . 6 rectangles are the minimum What command would draw this blue rectangle? art. add(Gr. Rectangle( );

Printing exactly this text: /------------------- | Welcome to the frugal arithmetician | -------------------/ Problem

Printing exactly this text: /------------------- | Welcome to the frugal arithmetician | -------------------/ Problem 3 37 hyphens here… 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. 11 spaces here… "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 ! | -------/ be sure not to use only combinations of d and x for these numbers

Problem 3 /* * Be sure to fill in this comment. . . */

Problem 3 /* * Be sure to fill in this comment. . . */ class CS 5 App { public static void main(String[] args) { int x = 5; Put the variables double d = 42. 0; at the top… H. pl(“/-------------------”); H. pl(“| Welcome to the frugal arithmetician |”); Lots more printing here } } remember:

int x = 5; double d = 42. 0; No other numbers! you’re "young"

int x = 5; double d = 42. 0; No other numbers! you’re "young" til 36 "pi" How do we print this? is 3. 14159 or this ? !?

Variable types int x = 5; double d = 42. 0; and many, many

Variable types int x = 5; 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 double d = 5/3. 0; H. pl( d ); Java’s default is

Formatting doubles double d = 5/3. 0; H. pl( d ); Java’s default is maximum precision. 1. 666666667 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);

What Java is thinking *%*/? ! double d = 5/3. 0; H. fmt(d, 4); H. pl( “five-thirds is ” + d ); five-thirds is 1. 666666667 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 H. pl(x) H. pl() H. p(x) prints x followed by a

H reference Output H. pl(x) H. pl() H. p(x) prints x followed by a newline. 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() H. nw() H. nd() H. nl() H. nc() H. nanyc() returns the next integer the user types or has typed. returns the next word the user types or has typed as a String. returns the next double the user types or has typed. returns the next line of text the user types as a String. returns the next char of text the user types as a char. 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/cs 5/HMCSupport. html

Problem 4 • AI program include at least 3 questions. . . /* *

Problem 4 • AI program include at least 3 questions. . . /* * Be sure to fill in this comment. . . */ class CS 5 App { 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!"); } }

Problem 4 -- putting the I in AI public static void main(String[] args) {

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? ”); String name = H. nl(); H. pl(name + "is ok, but I liked T 2"); } Why will Java complain about this code?

Problem 4 – mixing it up. . . public static void main(String[] args) {

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 T 1000”); H. pl("What’s your model number? "); int modelnum = H. ni(); // gets integer H. pl(“A ” + modelnum + “? ”); H. pl(“I thought those were extinct!”); }

you can’t type return inside a Java String. . . Style Matters. . .

you can’t type return inside a Java String. . . Style Matters. . . handling lots of text 1 keep lines to less than 80 chars long! or 2 3 4 H. pl(“Aha! I’ve tricked you. ” + fav. Dish + “ can’t be your favoriten” + “ Platt food, because Platt foodn” + “ does not exist!”); H. p(“Aha! I’ve tricked you. ”); H. pl(fav. Dish + “ can’t be your favorite”); H. pl(“ Platt food, because Platt food”); H. pl(“ does not exist!”); • be sure you consistently indent within code blocks • be sure you line up matching punctuation (curly braces) • 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/cs 5/ Submit

Submitting • Go to • Click on http: //www. cs. hmc. edu/courses/2004/fall/cs 5/ 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 CS 5 App. java file It is inside the source_code folder! • Be sure NOT to submit the CS 5 hw file … • Problems submitting ? Email me at dodds@cs. hmc. edu