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.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

TA: Nouf Al-Harbi NoufNaief.net :::
Road Map Introduction to object oriented programming. Classes
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
Reading Information From the User Making your Programs Interactive.
Introduction to methods Chapter 5: Classes and Objects in Depth.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Saravanan.G.
UML Basics & Access Modifier
Chapter 10 METHODS AND CONSTRUCTORS 1. Accessing Objects  Referencing the object’s data: objectReference.data myCircle.radius  calling the object’s.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
10-Nov-15 Java Object Oriented Programming What is it?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Defining a ClasstMyn1 Defining a Class A class is a template that defines the form of an object. It specifies both the data and the code that will operate.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Methods.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Fundamentals BCIS 3680 Enterprise Programming.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Creating Your Own Classes
CSC111 Quick Revision.
Classes and Objects.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Program: Shape Area Print the area for various shape types using the respective formulas.
Abstract Data Types Programmer-created data types that specify
Introduction to Methods in java
TK1114 Computer Programming
CS Week 13 Jim Williams, PhD.
ITunes Lab Copyright © 2012 Pearson Education, Inc.
More on Classes and Objects
Chapter 5: Classes and Objects in Depth
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Lecture 5- Classes, Objects and Methods
JAVA CLASSES.
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

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.

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.

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

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.

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.

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.

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.

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.

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