Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.

Similar presentations


Presentation on theme: "Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default."— Presentation transcript:

1 Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default one for you. Order of Class Composition: Import statement if needed 1.public class ClassName 2.private Field data (instance variables) 3.public Constructor (method that creates the object) 4.public Methods ( get and set the instance variables and other actions as necessary) Create Driver class that has a main to call the methods.

2 Create Triangle program Triangle program will have instance variables for the 3 sides. side1, side2, side3, perimeter and area It will have methods that will return information from the instance variables and compute the area and perimeter. Use the Scanner class to get user input to assign the three sides.

3 Create Triangle Class public class Triangle { private double side1; private double side2; private double side3; private double perimeter; private double area; Instance variables Instance variables are created in an area called a field. They are also referred to as field data. Every object created gets a copy of the variables when it is created. I could create triangle objects that could have different information for side1, side2, and side3 which would calculate a different area and perimeter. All instance variables are declared private

4 Constructor public Triangle() { } public Triangle(double s1, double s2, double s3) { side1 = s1; side2 = s2; side3 = s3; } A constructor specifies how to create an object. Java always has a default constructor for the program. If you do not create one it will supply it. However, you can create overloaded constructors to supply information through the parameter when the object is created. You will lose the default constructor when you do this. So it is a good idea to always create the default constructor. Format: visibility ClassName public Triangle() { // default empty parameter constructor } public Triangle(double s1, double s2, double s3) When you create the object the values you enter get assigned to the instance variables.

5 Create getter methods for the class public double getSide1() { return side1; } public double getSide2() { return side2; } public double getSide3() { return side3; } public double getArea() { double p = (side1 + side2 + side3) / 2; area = Math.sqrt(p(p-side1)(p-side2)(p-side3)); return area; } public double getPerimeter() { perimeter = side1 + side2 + side3; return perimeter; } public String toString() { return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2 + " Side 3 = " + side3 + “ Area: + area + “ Perimeter: + perimeter); } A getter method is called a return method. It returns data. It returns the information stored in the instance variables. Format: visibility returnType methodName() Since instance variables are private the only way to get the data from them is to create a return method.

6 Setter methods or void methods public void setSide1(double s) { side1 = s; } public void setSide2(double s) { side2 = s; } public void setSide3(double s) { side3 = s; } Void methods perform an action. The information passed through the parameter when the method is called is assigned to the instance variables.

7 Complete Class public class Triangle { double side1; double side2; double side3; double perimeter; double area; public Triangle(double s1, double s2, double s3) { side1 = s1; side2 = s2; side3 = s3; } public double setSide1() { return side1; } public double setSide2() { return side2; } public double setSide3() { return side3; } public void setSide1(double s) { side1 = s; } public void setSide2(double s) { side2 = s; } public void setSide3(double s) { side3 = s; } public double getArea() { double p = (side1 + side2 + side3) / 2; area = Math.sqrt(p(p-side1)(p-side2)(p-side3)); return area; } public double getPerimeter() { perimeter = side1 + side2 + side3; return perimeter; } public String toString() { return " Triangle: Side 1 = " + side1 + " Side 2 = " + side2 + " Side 3 = " + side3 + “ Area: + area + “ Perimeter: + perimeter); } } The toString() method is from object. It is overrident to print what you want it to print.

8 Driver Class import java.util.Scanner; public class TriangleDriver { public static void main(String[]args) { Scanner sc = new Scanner(System.in); System.out.println("Enter side 1 " ); double localSide1 = sc.nextDouble(); System.out.println("Enter side 2 " ); double localSide2 = sc.nextDouble(); System.out.println("Enter side 3 " ); double localSide3 = sc.nextDouble(); Triangle t = new Triangle(localSide1, localSide2, localSide3); System.out.println("The area is " + t.getArea()); System.out.println("The perimeter is " + t.getPerimeter()); } } A driver class is where the main method is located. You create an object of the class you want to use. You use the object to call the methods. If you want to get user input use the Scanner class to get the three sides of the Triangle. Create an object with those values. Use the object to call the method with the.operator Use the return methods inside a print statement.

9 Scanner Class import java.util.Scanner; // util package Constructor: public Scanner(Input Source) So to create a Scanner you do the following: Scanner sc = new Scanner(System.in); Scanner methods: sc.nextInt() stores ints sc.nextDouble() stores doubles sc.nextLine() stores next line as string sc.next() stores next as string


Download ppt "Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default."

Similar presentations


Ads by Google