1 / 18

Intro to CS – Honors I Classes and Methods

Intro to CS – Honors I Classes and Methods. Georgios Portokalidis gportoka@stevens.edu. Object-Oriented Programming. Objects They are all around us Cars, people, trees… Each object can perform certain actions Independently or interacting with other objects

ilya
Download Presentation

Intro to CS – Honors I Classes and Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to CS – Honors IClasses 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 Automobile • fuel: double • speed: double • license: String • 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 intgetAgeInHumanYears() • { • inthumanAge = 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. A class does not need to have instance variables • 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 "); • inthumanYears = 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 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 intgetAgeInHumanYears() • { • inthumanAge = 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 "); • inthumanYears = scooby.getAgeInHumanYears(); • System.out.println(humanYears+ " in human years."); • } • } Defining methods SYNTAX: public Return_TypeMethod_Name(Parameters) { Statements } Invoking a method Invoking a method and saving its return value

  9. Defining Methods Method heading • 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 body

  10. Defining Methods Method arguments Method heading • 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(); • } Local variables Method body

  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 Return statement SYNTAX: return Expression; Method return type • public intgetAgeInHumanYears() • { • inthumanAge = 0; • if (age <= 2) • { • humanAge = age * 11; • } • else • { • humanAge = 22 + ((age-2) * 5); • } • return humanAge; • } TIP Make your code more readable by using only one return statement Return value

  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 (inti = 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; • } • }

More Related