Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Recitation 9 / {06, 07} / 2007. Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September.

Similar presentations


Presentation on theme: "CS 180 Recitation 9 / {06, 07} / 2007. Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September."— Presentation transcript:

1 CS 180 Recitation 9 / {06, 07} / 2007

2 Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September 12 Make use of newsgroup Mail to Armand Navabi for Mentor session mailing list [anavabi@cs.purdue.edu]

3 Use of numeric data Use Parentheses –They help reduce errors –Make code more readable Eg: 6 + 15 / 3 or 6 + (15/3) or (6 + 15)/3

4 Operator Overloading When a symbol is used to represent more than one operation – “+” symbol – 6 + 3 = 9 – String output = “hello”+”kitty” Result will be “hellokitty” –“+” symbol is evaluated from left to right

5 Operator Overloading int x = 1; int y = 2; String output = “test” + x + y; output will be test12 String output = x + y + “test” output will be 3test How do we get test3 as the result?

6 Use of Constants Used when value of a variable remains constant Standard Java Convention –Use only capital letters and underscores Makes code more readable Allows user to make changes easily Constants defined in Java API: –Example: PI in Math class (package java.lang)

7 DecimalFormat Class Use DecimalFormat class (package is java.text) to format for numerical outputs double num = 123.45789345; DecimalFormat df = new DecimalFormat(“0.000”); //three decimal places System.out.print(num); System.out.print(df.format(num)); 123.45789345 123.458

8 Escape Characters Use of control characters –Tab \t –Newline \n

9 Math Class Available in java.lang package Make sure you get into the habit of using API for Java 5 Will find methods defined for a wide variety of mathematical and other programming needs

10 Math Class continued MethodInput type Output type Description exp(a) double Return e raised to power a. log(a) double Return natural log of a. floor(a) double Return largest whole number smaller than a. max(a,b) int double … int double … Return larger of a or b. pow(a,b) double Return a raised to power b. sqrt(a) double Return square root of a. sin(a) double Return sine of a(in radians).

11 Math Class Examples: –Formula: s = ut + 1/2 at 2 Code: s = u*t + a*t*t/2.0; –Formula: Code: v = Math.sqrt(3*k*t/(m*p)); –Formula: a = 2 π r 2 + 2 π r h Code: a = 2*Math.PI*r*r + 2*Math.PI*r*h;

12 GregorianCalendar Class Part of java.util.Date package Used to manipulate calendar information GregorianCalendar today, independenceDay; today = new GregorianCalendar(); independenceDay = new GregorianCalendar(1776, 6, 4); //month 6 means July; 0 means January

13 Constants in GregorianCalendar ConstantDescription YEARThe year portion of the calendar date MONTHThe month portion of the calendar date DATEThe day of the month DAY_OF_MONTHSame as DATE DAY_OF_YEARThe day number within the year DAY_OF_MONTHThe day number within the month DAY_OF_WEEKThe day number within the week (Sun --1, Mon -- 2, etc.) WEEK_OF_YEARThe week number within the year WEEK_OF_MONTHThe week number within the month AM_PMThe indicator for AM or PM (AM -- 0, PM -- 1) HOURThe hour in the 12-hour notation HOUR_OF_DAYThe hour in 24-hour notation MINUTEThe minute within the hour

14 Sample Calendar Retrieval GregorianCalendar cal = new GregorianCalendar(); //Assume today is Nov 9, 2003 System.out.print(“Today is ” + (cal.get(Calendar.MONTH)+1) + “/” + cal.get(Calendar.DATE) + “/” + cal.get(Calendar.YEAR)); Today is 11/9/2003 Output

15 Revisit JOption Pane Does this work? age is not a String. It is of type int. How do we handle this? int age; age = JOptionPane.showInputDialog(null, “Enter your age”);

16 Wrapper Classes Wrapper classes are used to perform necessary type conversions, such as converting a String object to a numerical value. int age; String inputStr; inputStr = JOptionPane.showInputDialog( null, “Enter your age”); age = Integer.parseInt(inputStr);

17 Other Conversion Methods Look in Java API to convert from –String to Double –String to Float –String to Long –Hint: You will find conversion from String to Double in the Double class of java.lang package in the API

18 One more time Use the newsgroup Start with Project #2 Mentoring


Download ppt "CS 180 Recitation 9 / {06, 07} / 2007. Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September."

Similar presentations


Ads by Google