Presentation is loading. Please wait.

Presentation is loading. Please wait.

Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

Similar presentations


Presentation on theme: "Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?"— Presentation transcript:

1 Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?

2 Objects and Classes PAP Computer Science Cycle 5

3 Review: Output/Input  Output  System.out.println( );  Input  int number = console.nextInt( );  double decimal = console.nextDouble( );  String word = console.next( );

4 Classes  Characterize an object  Category of information used to collect and contain properties/methods for related pieces of information  Objects are one instance of that class

5 Example Class: Superheroes  Class properties  Name, costume, colors, sidekick, main villain, powers, universe (Marvel vs DC)  Class methods  Fly, use a super power, talk, rescue a damsel in distress  Object instances  Superman, Spiderman, Wolverine, Flash

6 Jeroo Connection  Class properties  how many flowers is it holding, what direction is it facing  Class methods  hop( ), plant( ), pick( ), toss( ), turn( ), give( )  Object instances  Jeroo bobby = new Jeroo

7 Classes We’ve Seen So Far  String  String word = “some text”;  Scanner  static Scanner console = new Scanner(System.in)

8 Creating an Object  Recall…an object is an instance of a class Standard format: Class name = new Class(parameters) Example: Jeroo bobby = new Jeroo(2);

9 Keyword New  The keyword new is used when creating objects  It sets aside memory in the computer for the object  It only has to be used once per object creation, much like declaring a variable

10 Using an Object – Dot Operator  To access the properties and methods associated with a class, you first type the name of your object, followed by a dot (.), then put the command you want to run Jeroo bobby = new Jeroo( ); //create object bobby.hop( ); //use the object

11 Examples static Scanner console = new Scanner(System.in); int number = console.nextInt( ); String name = “Spring Break”; int length = name.length( ); float circumference = 2*radius*Math.PI

12 Review  Classes are a way to bundle the properties and methods of related information.  Class objects are instances of a class.  Use the operator new to create a class object.  In Java, the dot (.) separates the object name from the property or method name

13 Warm-Up: Wednesday, March 12  What is the difference between class properties and class methods?

14 Class Math Cycle 5 Week 4

15 Review  Classes are a way to bundle the properties and methods of related information.  Class objects are instances of a class.  Use the operator new to create a class object.  In Java, the dot (.) separates the object name from the property or method name

16 Math Operators  Basics  Addition (+)  Subtraction (-)  Multiplication (*)  Division (/)  Modulus (%)  Other math functions must be accessed using the Math class

17 Using class Math  The class Math is stored within the java.lang package  At the beginning of each code, be sure to add the following line: import static java.lang.Math.*  This allows you to drop the Math. prefix when using Math methods and constants

18 Math Constants  Math.E = 2.718281828459045  Math.PI = 3.141592653589793

19 Math Methods  abs(x) – absolute value abs(-96.5)  95.6  ceil(x) – ceiling operator ceil(54.13)  55  floor(x) – floor operator floor(54.13)  54  exp(x) – returns e x exp(3)  e 3  20.0855369232

20 Math Methods  log(x) – natural logarithm (base e) of x log(2)  ln(2)  0.69314718056  log10(x) – base-10 logarithm of x log10(100)  log 10 (100)  2.0  max(x,y) – maximum of two numbers max(15, 25)  25  min(x,y) – minimum of two numbers min(3, 4)  3

21 Math Methods  pow(x,y) – returns x y pow(2,3)  2 3  8  round(x) – rounds x to the nearest whole # round(34.4)  34 round(34.7)  35  sqrt(x) – square root of a number sqrt(121)  √121  11

22 import static java.lang.Math.*; public class MathProblems { public static void main(String[] args) { int x = pow(10, 3); System.out.println(“10^3 = “ + x); int m = max(24, 67); System.out.println(“Max of 24 and 67 = “ + m); double a = 2*PI*4; System.out.print(“Area of a circle with radius ”); System.out.print(“of 4 = “ + a); }

23 Warm-up: Thursday, March 13  What class are each of these packages associated with? a. java.util.* b. javax.swing.* c. java.lang.Math.*

24 Warm-up: Thursday, March 13  What class are each of these packages associated with? a. java.util.* Scanner b. javax.swing.* JOptionPane (pop-up boxes) c. java.lang.Math.* Math

25 Class String Cycle 5 Week 4

26 Strings - Review  Class used to manipulate character arrays, or words  Declared like variables  String name = “Alexander”;  Can be combined using the + operator  String break = “Spring ” + “break”;

27 String Methods  String.length( ) – returns the length of the string (how many letters long it is)  String word = “dog”; word.length( )  3  String.toLowerCase( ) – changes all capital letters to lowercase  String word = “HAHA”;  word.toLowerCase( )  “haha”;  String.toUpperCase( ) – changes all lowercase letters to capital letters  String word = “kitty”;  word.toUpperCase( )  “KITTY”

28 Strings as Lists  Strings are essentially a list of letters  Strings are indexed just like lists  String indices begin counting at 0 String word = “happy”; word[0] = ‘h’ word[1] = ‘a’ word[2] = ‘p’ word[3] = ‘p’ word[4] = ‘y’

29 More String Methods  String.charAt(x) – returns the letter at position x in the String  String word = “happy”;  word.charAt(2)  ‘p’;  String.replace(x,y) – replaces each instance of character ‘x’ with character ‘y’  String word = “happy”;  word.replace(‘p’,’ r’)  “harry”

30 indexOf( )  indexOf(char) – returns the index, or list location, of the first instance of letter char  String word = “happy”;  word.indexOf(‘h’)  0  indexOf(str) – returns the index, or list location, of the first instance of a String str  String name = “Alexander”;  name.indexOf(“and”)  4  Note : Both of these commands will return - 1 if the character or String is not found

31 String.substring( )  String.substring(x, y) – Converts the characters between list location x of the String (inclusive) and list location y of the String (exclusive) into a new String  String word = “Spring Break”;  word.substring(0,6)  “Spring”  word.substring(7, 12)  “Break”  word.substring(3, 9)  “ing Br”


Download ppt "Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?"

Similar presentations


Ads by Google