Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS

Similar presentations


Presentation on theme: "Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS"— Presentation transcript:

1 Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU

2 Object-Oriented Programming Objects ◦They are all around us ◦Cars, people, trees… Each object can perform certain actions ◦Independently or interacting with other objects ◦These actions are called methods Each objects has characteristics or attributes ◦Example: a car has static attributes like its color and dynamic attributes like its speed A class defines the type or kind of object ◦Example: all car objects could belong to the automobile class ◦A blueprint Objects belonging to a particular class have similar attributes, but are not the same!

3 A Class Outline The Unified Modeling Language can be used describe classes -fuel: double -speed: double -license: String Automobile +accelerate(double pedalPressure) +decelerate(double pedalPressure)

4 A Dog Class Use UML to describe such a class ◦What attributes would it have? ◦What methods?

5 public class Dog { public String name; public String breed; public int age; public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Instance variables Variables in the body of the class Can be accessed by all class methods SYNTAX public class Class_Name { Instance_Variable_Declarations... Method_Definitions }

6 public class DogDemo { public static void main(String[] args) { Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog scooby = new Dog(); scooby.name = "Scooby"; scooby.age = 42; scooby.breed = "Great Dane"; System.out.println(scooby.name + " is a “ + scooby.breed + "."); System.out.print("He is " + scooby.age + " years old, or "); int humanYears = scooby.getAgeInHumanYears(); System.out.println(humanYears + " in human years."); } Create a new Dog object using Java’s new operator new allocates a new set of instance variables for the object A class does not need to have instance variables Public instance variables can be set/read from other classes

7 public class Dog { public String name; public String breed; public int age; public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Method not returning any value Method returning an int

8 public class DogDemo { public static void main(String[] args) { Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog scooby = new Dog(); scooby.name = "Scooby"; scooby.age = 42; scooby.breed = "Great Dane"; System.out.println(scooby.name + " is a “ + scooby.breed + "."); System.out.print("He is " + scooby.age + " years old, or "); int humanYears = scooby.getAgeInHumanYears(); System.out.println(humanYears + " in human years."); } Invoking a method Invoking a method and saving its return value Defining methods SYNTAX: public Return_Type Method_Name(Parameters) { Statements }

9 Defining Methods public void writeOutput() { System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } Method heading Method body

10 Defining Methods public void writeOutput(int unusedArgument1, String unusedArgument2) { double unusedLocalVariable; System.out.println("Name: " + name); System.out.println("Breed: " + breed); System.out.println("Age in calendar years: " + age); System.out.println("Age in human years: " + getAgeInHumanYears()); System.out.println(); } Method heading Method body Method arguments Local variables

11 Understanding A Method Call Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; balto.writeOutput(); Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; System.out.println("Name: " + balto.name); System.out.println("Breed: " + balto.breed); System.out.println("Age in calendar years: " + balto.age); System.out.println("Age in human years: " + balto. getAgeInHumanYears()); System.out.println();

12 Defining Methods that Return Values public int getAgeInHumanYears() { int humanAge = 0; if (age <= 2) { humanAge = age * 11; } else { humanAge = 22 + ((age-2) * 5); } return humanAge; } Return value Method return type Return statement SYNTAX: return Expression; TIP Make your code more readable by using only one return statement

13 Using return in void Methods You can use it without a return value to alter control flow public void showLandPortion() { if (population == 0) { System.out.println("Population is zero."); return; // Ends here to avoid division by zero. } double fraction = 6.0 / population; System.out.println("If the population were spread "); System.out.println("over 6 continents, each " + "individual"); System.out.println("would have a fraction of its "); System.out.println("continent equal to " + fraction); } TIP Generally should be avoided. Code is cleared when using if.. else

14 Using the this Keyword Referring to an object’s public instance variables Object_name. Variable_name The this keyword can be used to refer to an object’s own instance variables from within its methods Not necessary in this case, but can help improve readability Dog balto = new Dog(); balto.name = "Balto"; balto.age = 8; balto.breed = "Siberian Husky"; public void writeOutput() { System.out.println("Name = " + this.name); System.out.println("Population = " + this.population); System.out.println("Growth rate = " + this.growthRate + "%"); }

15 Local Variables Local variables are only valid within the method they are declared Local variables can also be declared within a block or compound statement (between { }) ◦Only valid within that block We have seen local variables and instance variables. Are there any other kind of variables? public void showNewBalance() { double newAmount; newAmount = amount + (rate / 100.0) * amount; } System.out.println("With interest added, the new amount is $" + newAmount); }

16 Method Parameters Parameters can be used to write generic/customizable methods Within the method it can be used as a local variable Supplying arguments when calling methods can be done ◦Using constants myNumber.exponentiation(2); ◦Using other variables ◦int exponent = 4; ◦myNumber.exponentiation(exponent); Arguments are automatically cast to the right type, if the parameter is larger than the supplied value ◦byte→short→int→long→float→double class Number { public long base; public long exponentiation(int power) { long result = 1; for (int i = 0; i < power; i++) { result *= base; } return result; }

17 Primitive Types are Passed Using call-by-value Defining method parameters of primitive types is equivalent to declaring a local variable int exponent = 4; myNumber.exponentation(exponent); System.out.println(exponent); What will this print? class Number { public long base; public long exponentiation(int power) { long result = 1; while (power-- > 0) { result *= base; } return result; }

18


Download ppt "Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS"

Similar presentations


Ads by Google