View
46
Download
1
Category
Preview:
DESCRIPTION
Warm-up: Tuesday, March 11. In programming, what is the difference between an object and a class?. Objects and Classes. PAP Computer Science Cycle 5. Review: Output/Input. Output System.out.println ( ); Input int number = console.nextInt ( ); double decimal = console.nextDouble ( ); - PowerPoint PPT Presentation
Citation preview
Warm-up: Tuesday, March 11
In programming, what is the difference between an object and a class?
Objects and ClassesPAP Computer ScienceCycle 5
Review: Output/Input Output
System.out.println( );
Input int number = console.nextInt( ); double decimal = console.nextDouble( ); String word = console.next( );
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
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
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
Classes We’ve Seen So Far String
String word = “some text”;
Scanner static Scanner console = new
Scanner(System.in)
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);
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
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 objectbobby.hop( ); //use the object
Examplesstatic Scanner console = new Scanner(System.in);int number = console.nextInt( );
String name = “Spring Break”;int length = name.length( );
float circumference = 2*radius*Math.PI
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
Warm-Up: Wednesday, March 12
What is the difference between class properties and class methods?
Class MathCycle 5Week 4
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
Math Operators Basics
Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%)
Other math functions must be accessed using the Math class
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
Math Constants Math.E = 2.718281828459045
Math.PI = 3.141592653589793
Math Methods abs(x) – absolute valueabs(-96.5) 95.6
ceil(x) – ceiling operatorceil(54.13) 55
floor(x) – floor operatorfloor(54.13) 54
exp(x) – returns ex
exp(3) e3 20.0855369232
Math Methods log(x) – natural logarithm (base e) of xlog(2) ln(2) 0.69314718056
log10(x) – base-10 logarithm of xlog10(100) log10(100) 2.0
max(x,y) – maximum of two numbersmax(15, 25) 25
min(x,y) – minimum of two numbersmin(3, 4) 3
Math Methods pow(x,y) – returns xy
pow(2,3) 23 8
round(x) – rounds x to the nearest whole #round(34.4) 34 round(34.7) 35
sqrt(x) – square root of a numbersqrt(121) √121 11
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); }}
Warm-up: Thursday, March 13 What class are each of these packages
associated with?
a. java.util.*
b. javax.swing.*
c. java.lang.Math.*
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
Class StringCycle 5Week 4
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”;
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”
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’
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”
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
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”
Recommended