Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Notes – Basics (Ch1-6)

Similar presentations


Presentation on theme: "Lecture Notes – Basics (Ch1-6)"— Presentation transcript:

1 Lecture Notes – Basics (Ch1-6)
Yonglei Tao

2 A Class as Viewed by Its Client
public class Die { public Die (int s) { } public int cast () { Public interface and private implementation Encapsulation

3 Class Definition 1 import java.util.Random; 2 3 /**
3 /** 4 This class models a die that, when cast, lands on a random 5 face. 6 */ 7 public class Die 8 { 9 private Random generator; private int sides; 11 /** Constructs a die with a given number of sides. @param s the number of sides, e.g. 6 for a normal die */ public Die(int s) { sides = s; generator = new Random(); } 21

4 Class Definition (Cont.)
/** Simulates a throw of the die @return the face of the die */ public int cast() { return 1 + generator.nextInt(sides); } 30 }

5 Class Definition (Cont.)
1 /** 2 This program simulates casting a die ten times. 3 */ 4 public class DieSimulator 5 { 6 public static void main(String[] args) 7 { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); System.out.print(n + " "); } System.out.println(); } 17 }

6 Primitive and Reference Variables
int num1 = 10, num2; // primitive variables num2 = num1 + 2; Rectangle box1; // reference variable box1 = new Rectangle (5, 10, 20, 30); Rectangle box2 = box1; box2.translate (10, 20);

7 Constant Definition Declared in a method
final double NICKEL_VALUE = 0.05; Declared in a class public static final double LITERS_PER_GALON = 3.785;

8 Primitive Types Type Description Size
int The integer type, with range -2,147,483, ,147,483,647 4 bytes byte The type describing a single byte, with range 1 byte short The short integer type, with range 2 bytes long The long integer type, with range -9,223,372,036,854,775, ,223,372,036,854,775,807 8 bytes double The double-precision floating-point type, with a range of about ±10308 and about 15 significant decimal digits float The single-precision floating-point type, with a range of about ±1038 and about 7 significant decimal digits char The character type, representing code units in the Unicode encoding scheme boolean The type with the two truth values false and true 1 bit

9 Cast Convert an expression from one type to another int num = 125;
double bal; bal = num + 50; num = (int) bal; num = (int) (bal * 100) byte b1 = 1, b2 = 2; byte b3 = (byte) (b1 + b2);

10 Mathematical Methods Static methods Function Returns square root
Math.sqrt(x) square root Math.pow(x, y) power xy Math.exp(x) ex Math.log(x) natural log Math.sin(x), Math.cos(x), Math.tan(x) sine, cosine, tangent (x in radians) Math.round(x) closest integer to x Math.min(x, y), Math.max(x, y) minimum, maximum

11 Class String A sequence of Unicode characters String s = “Hello”;
char ch = s.charAt (2); s = “Goodbye”; System.out.println ( s.length() ); if ( s.equals(“Hello”) ) int pos = s.indexOf(“e”); System.out.println ( s.substring(pos, pos+2) );

12 String Concatenation “Hello, “ + name int n = 5; String s = “Section “ + n Date now = Date(); String s = “Hello, “ + now; String sub = s.substring(0, 5); String input = “7”; int num = Integer.parseInt ( input );

13 The if Statement if ( condition ) a single or compound statement else

14 Testing Simple test Test for floating point values int a = 5, b = 10;
if ( a == b ) … Test for floating point values double r = Math.sqrt(2); double d = r * r - 2; if (d == 0) System.out.println(“result is 0"); else System.out.println(“result is not 0 but " + d); Output ? result is not 0 but E-16

15 How to Compare? if Math.abs(d) < then

16 Testing Test for reference equality s2 == s3 Test for value equality
String s1 = new String(“abc”): String s2 = new String(“abc”); String s3 = s2; Test for reference equality s2 == s3 s1 == s2 s1 == null Test for value equality s1.equals (s2) boolean equals (Object other)

17 De Morgan’s Law ! ( A && B ) is the same as !A || !B

18 while Loops year = 0; while (balance < targetBalance) { double interest = balance * rate / 100; balance = balance + interest; years++; }

19 for Loops for (int i = 0; i < n; i++)
{ double interest = balance * rate / 100; balance = balance + interest; }

20 Read Input Scanner in = new Scanner (System.in);
System.out.print (“Your age: “); int age = in.nextInt (); nextBoolean() hasNextBoolean() nextByte() hasNextByte() nextDouble() hasNextDouble() nextFloat() hasNextFloat() nextLong() hasNextLong() nextShort() hasNextShort() next() useDelimiter(“;")

21 Read Character with Scanner
Scanner in = new Scanner (new File(“input.txt”)); while ( in.hasNextLine() ) { String line = in.nextLine(); char[ ] list = line.toCharArray(); for (char ch: list) // a for-each loop // process ch } Or String line = in.nextLine(); for (int i=0; i<line.length(); i++) // process line.charAt(i)

22 Keyboard Output Other format specifiers and flags
System.out.println(“Your age is: “ + age); System.out.printf(“Your age is: %d \n“, age); System.out.printf(“Pi is %7.3f \n“, Math.Pi); Other format specifiers and flags %s, %c %s^ convert to upper case %d- left justified %f, show commas to separate each group of 3 digits %f+ include a sign %d( show negative values in parentheses

23 Documentation Comments
/** A driver program for Greeter.java. It also shows java documentation comments. @author Yonglei Tao, GVSU */ public class GreeterTest { public static void main(String[] args) { Greeter worldGreeter = new Greeter("World"); String greeting = worldGreeter.sayHello(); System.out.println(greeting); }

24 Documentation Comments
/** A class for producing simple greetings. */ public class Greeter { private String name; Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. @throws Exception in case of ... public Greeter(String aName) { name = aName; } Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. public String sayHello() { return "Hello, " + name + "!";


Download ppt "Lecture Notes – Basics (Ch1-6)"

Similar presentations


Ads by Google