Download presentation
Presentation is loading. Please wait.
Published byΚλεόπατρος Κωνσταντίνου Modified over 6 years ago
1
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 CS 5 website
2
Tutors available -- contact information
3
Tutors available -- contact information Available in Parsons PC Labs (the CIS labs) Lab Code: Blg Code: Available in the LAC Lab (Linde Activities Center) Blg Code: Starting next week... You may also seek out tutors away from the labs, by phone, in the dorms, etc.
4
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.
5
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...
6
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); }
7
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!
8
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
9
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( );
10
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 / \ | Bye ! | \ /
11
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+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); Extra Credit: In as few operations as possible!
12
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 / \ | Bye ! | \ / 37 hyphens here… 11 spaces here… be sure not to use only combinations of d and x for these numbers
13
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: \
14
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 or this ?!?
15
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 !
16
Maybe a little less precision…
Java’s default is maximum precision: double d = 5/3.0; H.pl( d ); H has some built-in formatting commands: H.pl( H.fmt(d) ); 1.667 H.pl( H.fmt(d,4) ); 1.6667
17
What Java is thinking *%*/?!
double d = 5/3.0; H.fmt(d,4); H.pl( “five-thirds is ” + d ); five-thirds is 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
18
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
19
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...
20
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?
21
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!”); }
22
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
23
Submitting Go to Click on
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 ? me at
24
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 CS 5 website
25
Tutors available -- contact information Available in Parsons PC Labs (the CIS labs) Lab Code: Blg Code: Available in the LAC Lab (Linde Activities Center) Blg Code: Starting next week... You may also seek out tutors away from the labs, by phone, in the dorms, etc.
26
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.
27
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...
28
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); }
29
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
30
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( );
31
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 / \ | Bye ! | \ / 37 hyphens here… 11 spaces here… be sure not to use only combinations of d and x for these numbers
32
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: \
33
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 or this ?!?
34
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 !
35
Formatting doubles Java’s default is maximum precision.
double d = 5/3.0; H.pl( d ); H does have some built-in formatting commands: H.pl( H.fmt(d) ); 1.667 H.pl( H.fmt(d,4) ); 1.6667
36
What Java is thinking *%*/?!
double d = 5/3.0; H.fmt(d,4); H.pl( “five-thirds is ” + d ); five-thirds is 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
37
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
38
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...
39
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?
40
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!”); }
41
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
42
Submitting Go to Click on
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 ? me at
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.