Download presentation
Presentation is loading. Please wait.
Published byThomas Walsh Modified over 9 years ago
1
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014
2
So we have the foundation needed to move on to new things! 2 Review of Week 3
3
Readability
4
You can’t change the value. Use the key-word final. Uses a different naming convention. Helps readability. Instead of: int num2 = num1 + 7; final int NUM2MODIFIER = 7; int num2 = num1 + NUM2MODIFIER;
5
To improve readability, you can often remove repetitive code, for example: Instead of: double quadratic1 = (-b + Math.sqrt(b*b – 4*a*c))/(2*a); double quadratic2 = (-b - Math.sqrt(b*b – 4*a*c))/(2*a); Use: double root = Math.sqrt(b*b – 4*a*c); double quadratic1 = (-b + root)/(2*a);
6
A method is overloaded if there are two or more methods with the same name, but different data types for the parameters and/or number of parameters Allows a method to perform similar tasks with different inputs Example: The System.out.println method is overloaded so it can print different types of data: Strings, ints, booleans, doubles, etc. 6
7
Say we have two methods: public static double mean(int a, int b) { … } public static double mean(int a, int b, int c) { … } The compiler will match up a method call to the correct method implementation based on the count and types of the given parameters mean(2, 3) will call the first method mean(5, 7, 1) will call the second method mean(1) will result in an error, as there is no matching method for mean(int) mean(1.0) will also be an error: no method mean(double) 7
9
Primitives store by value Objects store by reference A reference says where the object is in memory: the actual object is somewhere else In this example, a stores the value of the int, 25, while s stores the address of where the String object is in memory, 83. 83 is NOT the value of the String! 9 int a 25 String s 83
10
Stores variables in objects Looks like: access static type name; i.e. private double GPA; Assigned values in constructor, mutator methods Accessed by accessor methods, or is public.
11
Used to create new objects. Looks like: public ClassName(parameters) { varName1 = myVarName; } Called by: ClassName name = new ClassName(parameters);
12
Does things: Looks like: access static returnType name(parameters) { //code goes here } Called by: objectName.methodName(parameters);
13
Accessors access values, returning instance variables / other values. Mutators mutate values, modifying an instance variable in the object.
14
static refers to whether the method or variable belongs to the class, and is the same, or static, throughout all instances of the class.
15
Now that we know how to use classes, we’re going to test them, without a main method directly inside the class. Is another class that is usually called ClassNameTester.
16
Testers contain a main method, then construct an object of the class it is testing. It then uses the methods to test and see if it works.
17
What variables would our cow need to store? weight – a double isAwake – a boolean What methods would our cow need to do? getWeight getAwake eat wakeUp goToSleep getMilk
18
Instance Fields: // how much the cow weighs in pounds private double weight; // true if the cow is awake; false if the cow is sleeping private boolean isAwake; Constructor: public Cow(double theWeight, boolean awake) { weight = theWeight; isAwake = awake; }
19
Methods: public double getWeight() { return weight; } public boolean isAwake() { return isAwake; }
20
public void wakeUp() { isAwake = true; System.out.println("streeeeetch..."); } public void goToSleep() { isAwake = false; System.out.println("zzzzzz..."); }
21
//This method milks the cow for a given number of seconds. We assume that each //gallon of milk weighs 7 pounds, and that a cow will give one gallon of milk per minute of //milking. It is not terribly realistic in that it lets you convert the entire weight of the cow //into milk, but we're dealing with virtual cows here, so whatever... public double getMilk(int seconds) { // we cannot get milk from a cow if it is asleep! if (!isAwake) return 0.0; //compute the max # of gallons the cow can give double gallonsToGive = weight / 7.0; //compute the # of gallons we want from the milking time double gallonsWeWant = seconds / 60.0; // We actually get the minimum of those two values double gallonsWeGet = Math.min(gallonsToGive, gallonsWeWant); weight -= gallonsWeGet * 7.0;// adjust the cow's weight return gallonsWeGet;// return the milk }
22
Tester: public class CowTester { public static void main(String[] args) { // bubba is born (we create a new object of type Cow) Cow bubba = new Cow(445.0, false); bubba.wakeUp(); // bubba eats 35 pounds of feed bubba.eat(35.0); // milk bubba for 4 mins (240 secs) double gals = bubba.getMilk(240); System.out.println("Bubba gave us " + gals + " gallons of milk!"); bubba.goToSleep(); System.out.println("Bubba now weighs " + bubba.getWeight() + " pounds."); }
23
Make a class box with instance variables length, width, height, isOpen, and material(String). It should contain accessors/mutators for all of the above, and the methods getVolume, getSurfaceArea, and toString, which returns a String representation of the box. Also, make a tester.
24
Create a class Tuple that does the following A constructor with two parameters for first and second. Has 2 integer parameters, first and second that can be accessed publically. Has a method that returns the product of the two numbers Has a method that returns the larger of the two numbers Has a method that takes in another tuple and adds that tuples’ first to its first and that tuples’ second to its’ second. 24
25
Create a class dayIn2014 The class with have instance variables day and month, and static variable year. Create constructor that takes a month and date as parameters Create assessors and mutators methods for all changeable variables Create a static method that take in two dates as parameters and returns true if the first date is earlier than the second and vice versa 25
26
Create a class coordinate Should have two values, x and y. Have a constructor A method that calculates the distance from the origin Accessor methods for x and y. Mutator methods addToX and addToY that increase the value of X and Y by the parameter. 26
28
Time to move on! 28
29
Interfaces
30
Inheritance is when a class takes all the methods and variables of another class(superclass) for itself and adds methods and variables that weren’t in the original. Each class can have one superclass, and an infinite amount of subclasses. You implement a subclass by adding extends ClassName in the class declaration For Example: public class MountainBike extends Bicycle
31
Class Vehicle What variables? ▪ weight ▪ wheels ▪ Miles ▪ On or off What methods? ▪ drive(distance)
32
Create a subclass of Vehicle (maybe Car, Bike, Truck?) with its own methods/variables e.g. turnOn() refuel() miles per gallon yPos Etc.
33
Interfaces are a way to create a standard to how different classes interact Interfaces can only contain constants, method signatures, and nested types Interfaces have empty methods that are only declared. These are called abstract methods. Interfaces are implemented in classes or extended in other interfaces For example: public class MountainBike implements Wheeled
34
All of an interfaces methods are abstract An Abstract class has at least one method that is non abstract 34
35
Interface Teleportable What method(s)? teleport(double newX)
36
Create a new class that extends your vehicle subclass, and implements the teleportable interface.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.