Homework 2 Sun., 9/12 Due Mon., 9/13 Problems available on-line

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Homework 6 Due ( MT sections ) ( WTh sections ) about midnight Sun., 10/12 Mon., 10/13 Problems
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
Need your MyMathLab card with your access code Need a Valid Address Need to know Purdue’s zip code is and your course ID for your Class You.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CS107 Introduction to Computer Science Java Basics.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Java: Chapter 1 Computer Systems Computer Programming II Aug
Java: Chapter 1 Computer Systems Computer Programming II.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Java Basics.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
CSC204 – Programming I Lecture 4 August 28, 2002.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Programming Concept Chapter I Introduction to Java Programming.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems
Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/21 Mon., 9/22 Problems
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Nelson Series Talk Wed, 9/15 7:00 pm in Galileo Macalister Transforming Mice into Men Despite some differences in appearance and habits, men and mice are.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Introduction to Java Import Scanner class to use in our program
Week 2 - Wednesday CS 121.
Katherine Kampf / kkampf
Content Programming Overview The JVM A brief look at Structure
CS0007: Introduction to Computer Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to Python
Introduction to Computer Science / Procedural – 67130
Eclipse Navigation & Usage.
USING ECLIPSE TO CREATE HELLO WORLD
Editing Your Faculty Homepage
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
CompSci 230 Software Construction
Adding Assignments and Learning Units to Your TSS Course
Intro to Java.
Macrosystems EDDIE: Getting Started + Troubleshooting Tips
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
String Output ICS 111: Introduction to Computer Science I
How to Run a Java Program
MSIS 655 Advanced Business Applications Programming
Loops CIS 40 – Introduction to Programming in Python
How to Run a Java Program
CS150 Introduction to Computer Science 1
elementary programming
Programming in JavaScript
Programming in JavaScript
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Variables Lesson Outline
In this class, we will cover:
EECE.2160 ECE Application Programming
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
CS31 Discussion 1D Fall18: week 2
Week 1 - Friday COMP 1600.
Presentation transcript:

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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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+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+x+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