DOC PREVIEW
UW-Madison CS 302 - Assignment

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Review: 3.1-3.5Assignments- Read for Wednesday: 4.4-4.6- Today 11:58 PM CodeLab #2o Questions?- Fri 9/24 CodeLab #3o Should be able to do some; will cover next week: Infer class declaration from sample programs and CodeWarrior Declaring constants Standard inputo Distinction between “expression” and “statement” o Questions?- Fri 10/1 Assignment 1Review: 2.1- Library, float: which is reference and which is primitive?o Library is reference and float is primitive- I want to use the method pump(lbs) to pump 10 pounds into the tire object t1. What are the three steps I need to take to accomplish this?o Tire t1;o t1 = new Tire();o t1.pump(10);- Suppose that objects of the Tire class start out flat. If I had declared t2 at the same time as t1, and then set it to t1 before I pumped t1, how many pounds would t2 have at the end of the code block?o t2 would have 10 pounds, because it shares its data object with t1.CS 302 Lecture 6 09171- Consider this code block: int i1, i2; i1 = 1; i2 = i1; i1 = 2;What is the value of i2 at the end?o i2 still has the value 1 because i1 and i2 have their own copies of the data.Review: 2.2- How would I add a block comment at the top of our Tire program that says, “This block of code demonstrates sharing of reference data”?o /* This block of code demonstrates sharing of * reference data*/- How would I comment our second code block with a single line saying, “Primitives are independent”?o // Primitives are independent- Suppose Tire’s fully qualified name is cs302.moravan.Tire. What line could we add to the top of our program file to avoid typing in this whole phrase every time we needed to reference the Tire class?o import cs302.moravan.*;- Right now we just have a couple of code blocks. How would we put them in a class called “RefVsPrim”?o class RefVsPrim {}- Something’s wrong with things as they stand. What are we missing?o The main method.- How would we add this?o public static void main(String[] args) {}CS 302 Lecture 6 09172Review: 2.3- Suppose we wanted to run our program in a real IDE. What name should we use to save this file?o RefVsPrim.java- What’s the next step that we need to do before we can actually run our program?o Compilation- What’s the name of the file containing our compiled code?o RefVsPrim.class- What’s the special word we use for compiled Java code?o Bytecode- What’s an example of an error that would prevent compilation?o Leaving off a semicolon, misspelling a key word…- What kind of errors can we still get?o Runtime errors (detected by interpreter)o Example: JFrame jf = null; jf.setVisible(true);CS 302 Lecture 6 091732.4 Simple Java Standard Classes- Stringo Explicit use of new optional; draw mem diagramo Substring First argument- Beginning position- Count from 0- Is displayed Second argument- End position- Is not displayed Creates new string Original string left intact Generates error if given illogical arguments Examples- “together”o 0-2= “to”o 2-5= “get”o 5-8= “her”- “michelle”: 3-7= “hell”o Length Return # of characters in a String Exampleso IndexOf Locate index position of substring within another string Returns position of first char of substring Returns -1 if substring not found Case-sensitiveCS 302 Lecture 6 09174 Returns first occurrence if multiple exists- Dateo Time instanceo Millisecond precisiono Automatically set to time created (from OS)o toString method converts to human-readable: Sat Apr 20 15:05:18 PDT 2002o internally represented as time since epoch (Jan 1, 1970 00:00:00 GMT)o Use GregorianCalendar for real dates- SimpleDateFormato Change display format of Dateo Pass formatiting string when create instance of SimpleDateFormato Formatting string case-sensitiveSimpleDateFormat simpleDF;simpleDF = new SimpleDateFormat(“…”);System.out.println(simpleDF.format(new Date()));- JoptionPane for inputo showInputDialog methodo Access input by assigning return result to String variableo Returns null if cancel clickedo Returns empty string if nothing entered & OK clickedCS 302 Lecture 6 091752.5 Sample Development- Get into the habit of developing according to the software life cycle stages (even though not strictly necessary for small programs)- Map out overall plano ID classes necessaryo ID implementation steps to followo Outline program logico Write design document- Consider design alternativeo Better depends on metrico Always developing for usersReview: 3.1-3.5- Declare and initialize a byte and a shorto byte b = 0;o short s = 1;- How do I add these and the constant 2 into and integer?o int i = b + s + 2;o Don’t have to do casting because of implicit promotion- How would I create a long with a value 34 times that of i?o long l = 34 * i;- How do I divide l by the float 5.67?o l / 5.67f;- What happens if we assign this expression to l?CS 302 Lecture 6 09176o Error: we’d have to cast the float to a long.- How could we fix this?o l = (long) (l / 5.67f);- What’s are two types we could assign it to, without casting?o double and float- What do we get if we execute i = i + 1 – 2 * 3 / 4 % 5?o i- What if we did i = (i+ 1) – (2 * (3 / 4 % 5) );o i + 1- What if we did d = -9 / 2 + 15 * 3 - -8?o d = 48- How can we fix this so that we get our fraction?o d = -9d / 2 + 15 * 3 - -8;- How can we declare a constant that represents the gravitational constant G from physics, which has the value 6.67 * 10^-11;o final double G = 6.67e-11;- Using the gravity equation F = G*m1*m2/ r^2, write a program that uses JOptionPane to get the inputs m1, m2, and r from the user. Then display the resulting F in the format “F = x” to the standard output window.CS 302 Lecture 6 09177/*Sample Program: Calculating the force of attraction between masses.*/import javax.swing.*;class Gravity {public static void main( String[] args ) {final double G = 6.67e-11;double m1, m2, r, force;String temp;temp = JOptionPane.showInputDialog(null, "Enter the first mass:");m1 = Double.parseDouble(temp);temp = JOptionPane.showInputDialog(null, "Enter the second mass:");m2 = Double.parseDouble(temp);temp = JOptionPane.showInputDialog(null, "Enter the radius between the masses:");r = Double.parseDouble(temp);force = G * m1 * m2 / (r * r);System.out.println("The resultant force is: " + force);}}CS 302 Lecture 6 091783.6 Standard Input- System.ino Object used to perform standard inputo Instance of InputStream classo Can only read one byte at a time-


View Full Document

UW-Madison CS 302 - Assignment

Download Assignment
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assignment and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assignment 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?