1 / 20

OLD BUSINESS Exam date? Other exam details? Is there a need for extra help sessions?

Class 15 Oct 21. OLD BUSINESS Exam date? Other exam details? Is there a need for extra help sessions? Breecher vs. Photoshop show moveable program “Where the fun never stops” Go over homework – string problems – do Show & Tell NEW BUSINESS:

naida
Télécharger la présentation

OLD BUSINESS Exam date? Other exam details? Is there a need for extra help sessions?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 15 Oct 21 OLD BUSINESS Exam date? Other exam details? Is there a need for extra help sessions? Breecher vs. Photoshop show moveable program “Where the fun never stops” Go over homework – string problems – do Show & Tell NEW BUSINESS: Continuing Chapter 3 today  random and math classes, & Formatted Output. Assignment for next time

  2. For Class 16 On Monday Oct 25 • Assignment for next time. • Read chapter 3 during the course of the next two weeks – keep up with where we are in class. • Do problem PP3.3 on page 156. You will need to read the text to solve this – I’m not going to talk about it in class. • Do problem PP3.6 on page 157

  3. Outline • Chapter 2 -- Data and Expressions • Chapter 3 – Using Classes and Objects • Creating objects • The string class • Packages • The random class • The math class • Formatting output • Enumerated types • Wrapper classes • Components and containers • Nested panels • Images • Chapter 4 – Writing Classes and Objects • Brain Stuff Class 14, Monday, 10/18 Class 15, Thursday, 10/21 Class 16, Monday, 10/26 Class 17, Thursday, 10/28 Exam 3-3

  4. Outline Creating Objects The String Class Packages Random Math Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

  5. Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing Class Libraries and Packages • A class library is a collection of classes that we can use when developing programs • The Java standard class library is part of any Java development environment. Includes classes we've already used (System , Scanner, String). • The classes of the Java standard class library are organized into packages • Some of the packages in the standard class library are:

  6. The import Declaration • When you want to use a class from a package, you could use its fully qualified name java.util.Scanner scan = new java.util.scanner(“…”); • Or you can import the class, and then use just the class name import java.util.Scanner; ------ Scanner scan = new Scanner(“….”) • To import all classes in a particular package, you can use the * wildcard character import java.util.*;

  7. The Random Class • The Random class is part of java.util package • Contains methods that generate pseudorandom numbers • A Random object performs calculations based on a seed value to produce a stream of “seemingly” random values

  8. The Random Class Example import java.util.Random; public class RandomNumbers { public static void main (String[] args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = generator.nextInt(10); System.out.println ("From 0 to 9: " + num1); num1 = generator.nextInt(10) + 1; System.out.println ("From 1 to 10: " + num1); num1 = generator.nextInt(15) + 20; System.out.println ("From 20 to 34: " + num1); num2 = generator.nextFloat(); System.out.println ("A random float (between 0-1): " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int)num2 + 1; System.out.println ("From 1 to 6: " + num1); } }

  9. The Math Class • The Math class is part of the java.lang package • Methods include: • absolute value • square root • exponentiation • trigonometric functions value = Math.cos(90) + Math.sqrt(delta);

  10. Quadratic.java import java.util.Scanner; public class Quadratic { public static void main (String[] args) { int a, b, c; // ax^2 + bx + c double discriminant, root1, root2; Scanner scan = new Scanner (System.in); System.out.print ("Enter the coefficient of x squared: "); a = scan.nextInt(); System.out.print ("Enter the coefficient of x: "); b = scan.nextInt(); System.out.print ("Enter the constant: "); c = scan.nextInt(); // Use the quadratic formula to compute the roots. discriminant = Math.pow(b, 2) - (4 * a * c); root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a); root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a); System.out.println ("Root #1: " + root1 + “ Root #2: " + root2); } }

  11. Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

  12. Enumerated Types • Java allows you to define an enumerated type, which can then be used to declare variables • An enumerated type establishes all possible values for a variable of that type • The values are identifiers of your own choosing • The following declaration creates an enumerated type called Season enum Season {winter, spring, summer, fall}; • Any number of values can be listed

  13. Enumerated Types • Once a type is defined, a variable of that type can be declared Season time; and it can be assigned a value time = Season.fall; • The values are specified through the name of the type • Enumerated types are type-safe – you cannot assign any value other than those listed

  14. Enumerated Types • The declaration of an enumerated type is a special type of class, and each variable of that type is an object • The ordinal method returns the ordinal value of the object • The name method returns the name of the identifier corresponding to the object's value

  15. IceCream.java // Demonstrates the use of enumerated types. public class IceCream { enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough} public static void main (String[] args) { Flavor cone1, cone2, cone3; cone1 = Flavor.rockyRoad; cone2 = Flavor.chocolate; System.out.println ("cone1 value: " + cone1); System.out.println ("cone1 ordinal: " + cone1.ordinal()); System.out.println ("cone1 name: " + cone1.name()); System.out.println (“\ncone2 value: " + cone2); System.out.println ("cone2 ordinal: " + cone2.ordinal()); System.out.println ("cone2 name: " + cone2.name()); cone3 = cone1; System.out.println (“\ncone3 value: " + cone3); System.out.println ("cone3 ordinal: " + cone3.ordinal()); System.out.println ("cone3 name: " + cone3.name()); } }

  16. Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images 3-16

  17. Wrapper Classes The problem to be solved – how do you convert a variable from one type to another. The java.lang package contains wrapper classes that correspond to each primitive type: 3-17

  18. Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); Because Integer is an object, it has methods that can be used on the value it contains. For example, the Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str); The wrapper classes often contain useful constants as well For example, the Integer class contains MIN_VALUE and MAX_VALUE which hold the smallest and largest int values 3-18

  19. Autoboxing – from Primitive to Wrapper How do you convert a primitive type to a wrapper object? Called Autoboxing Here’s an example: Integer obj; int num = 42; obj = num; The assignment creates an Integer object You can also do unboxing) num = obj; 3-19

  20. Autoboxing – from Primitive to Wrapper Let’s look at some examples: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html import java.lang.*; public class Wrapper { public static void main (String[] args) { Double dblwrap1 = new Double("3.14159"); // Constructor Double dblwrap2 = new Double(Math.PI); // Constructor double dblprim = 2.71828459045; Integer intwrap; int intprim; System.out.println ("int of dblwrap1 = " + dblwrap1.intValue() ); System.out.println ("Is dblwrap1 the same as dblwrap2?? " + dblwrap1.compareTo(dblwrap2 ) ); System.out.println ("Is dblwrap1 the same as dblwrap2?? " + dblwrap1.compareTo(dblwrap2 ) ); System.out.println ("Is dblwrap1 the same as dblwrap2?? " + dblwrap1.equals(dblwrap2) ); dblprim = Math.sqrt(82.0); dblwrap1 = dblprim; System.out.println ("Double value is "+dblwrap1+ " integer value of this = " + dblwrap1.intValue() ); } } 3-20

More Related