1 / 20

Outlines

University of Palestine software engineering department Introduction to data structures Introduction to Classes and Objects instructor: Tasneem Darwish. Outlines. Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a Class

Download Presentation

Outlines

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. University of Palestinesoftware engineering departmentIntroduction to data structuresIntroduction to Classes and Objectsinstructor: Tasneem Darwish

  2. Outlines • Classes, Objects, Methods and Instance Variables • Declaring a Class with a Method and Instantiating an Object of a Class • Declaring a Method with a Parameter • Instance Variables, set Methods and get Methods • Primitive Types vs. Reference Types • Initializing Objects with Constructors • Floating-Point Numbers and Type double

  3. Primitive Types vs. Reference Types • Data types in Java are divided into two categories—primitive types and reference types (sometimes called nonprimitive types). • The primitive types are boolean, byte, char, short, int, long, float and double. • All nonprimitive types are reference types, so classes, which specify the types of objects, are reference types

  4. Primitive Types vs. Reference Types • A primitive-type variable can store exactly one value of its declared type at a time. • For example, an int variable can store one whole number at a time. • Primitive-type instance variables are initialized by default—variables of types byte, char, short, int, long, float and double are initialized to 0, and variables of type boolean are initialized to false. • local variables are not initialized by default.

  5. Primitive Types vs. Reference Types • Programs use variables of reference types (normally called references) to store the locations of objects in the computer’s memory. • A reference to an object is required to invoke (i.e., call) the object’s methods. • Note that primitive-type variables do not refer to objects, so such variables cannot be used to invoke methods

  6. Initializing Objects with Constructors • Each class you declare can provide a constructor that can be used to initialize an object of a class when the object is created. • In fact, Java requires a constructor call for every object that is created. • Keyword new calls the class’s constructor to perform the initialization. • the constructor must have the same name as the class

  7. Initializing Objects with Constructors • By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor. • When a class has only the default constructor, its instance variables are initialized to their default values.

  8. Initializing Objects with Constructors • When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class • For example, a programmer might want to specify a course name for a GradeBook object when the object is created, as in

  9. Initializing Objects with Constructors • Like methods, constructors also can take arguments. • However, an important difference between constructors and methods is that constructors cannot return values, so they cannot specify a return type (not even void). • Normally, constructors are declared public. • If a programmer declares any constructors for a class, the Java compiler will not create a default constructor for that class.

  10. Initializing Objects with Constructors

  11. Floating-Point Numbers and Type double • Java provides two primitive types for storing floatingpoint numbers in memory float and double. • The primary difference between them is that double variables can store numbers with larger magnitude and finer detail (i.e., more digits to the right of the decimal point—also known as the number’s precision) than float variables.

  12. Floating-Point Numbers and Type double • Variables of type float represent single-precision floating-point numbers and have seven significant digits. • Variables of type double represent double-precision floating-point numbers. These require twice as much memory as float variables and provide 15 significant digits

  13. Floating-Point Numbers and Type double

  14. Floating-Point Numbers and Type double

  15. Floating-Point Numbers and Type double • The format specifier %f is used to output values of type float or double. • The .2 between % and f represents the number of decimal places (2) that should be output to the right of the decimal point in the floating-point number—also known as the number’s precision

  16. Floating-Point Numbers and Type double

  17. Floating-Point Numbers and Type double

  18. Floating-Point Numbers and Type double

  19. H.W group 1 • Create a class called Employee that includes three pieces of information as instance variables— a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

  20. H.W group 2 • Create a class called Date that includes three pieces of information as instance variables—a month (type int), a day (type int) and a year (type int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Date’s capabilities.

More Related