Classes One class usually represents one type of object

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
Department of Computer Science
Shorthand operators.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
1 Operators and Expressions Instructor: Mainak Chaudhuri
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Mixing integer and floating point numbers in an arithmetic operation.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Classes One class usually represents one type of object –May contain its own member variables –May contain its own methods to operate on the member variables.
L EC. 06: C LASS D ETAILS S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java: Base Types All information has a type or class designation
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
2015 Spring Content Class method [review] Access control
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Department of Computer Science
using System; namespace Demo01 { class Program
Chapter 2 Elementary Programming
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 2.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Computing Adjusted Quiz Total Score
Pass by Reference, const, readonly, struct
Interface.
Object-Oriented Programming
Inner Classes 29-Nov-18.
Implementing Classes Chapter 3.
Recursive GCD Demo public class Euclid {
JAVA Constructors.
Take out a piece of paper and PEN.
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
© A+ Computer Science - OOP Pieces © A+ Computer Science -
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Scope of variables class scopeofvars {
See requirements for practice program on next slide.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Introduction to Object-Oriented Programming
Developing Java Applications with NetBeans
Inner Classes 1-May-19.
Developing Java Applications with NetBeans
Names of variables, functions, classes
Dr. R Z Khan Handout-3 Classes
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Consider the following code:
Inner Classes 25-Oct-19.
class Box { double width; double height; double depth; }
Presentation transcript:

Classes One class usually represents one type of object May contain its own member variables May contain its own methods to operate on the member variables Usually one class is defined in one Java file In the entire program exactly one class should contain a main method

Vehicle class /* A program that uses the Vehicle class. Call this file VehicleDemo.java */ class Vehicle { int passengers; // number of passengers int fuelCapacity; // fuel capacity in litres int kmperlitre; // fuel consumption in kilometer per litre }

// This class declares an object of type Vehicle. class VehicleDemo { Contd. // This class declares an object of type Vehicle. class VehicleDemo { public static void main(String args[]) { Vehicle minivan = new Vehicle(); double range; // assign values to fields in minivan minivan.passengers = 7; minivan.fuelCapacity = 16; minivan.kmperliter = 21; // compute the range assuming a full tank of gas range = minivan.fuelCapacity * minivan.kmperliter; System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range+" kms"); }

Contd. // This program creates two Vehicle objects. class Vehicle { int passengers; // number of passengers double fuelCapacity; // fuel capacity double kmperliter; // fuel consumption } // This class declares an object of type Vehicle. class TwoVehicles { public static void main(String args[]) { Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); double range1, range2; // assign values to fields in minivan minivan.passengers = 7; minivan.fuelCapacity = 16; minivan.kmperliter = 21; // assign values to fields in sportscar sportscar.passengers = 2; sportscar.fuelCapacity = 14; sportscar.kmperliter = 12; // compute the ranges assuming a full tank of gas range1 = minivan.fuelCapacity * minivan.kmperliter; range2 = sportscar.fuelCapacity * sportscar.kmperliter; System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range1); System.out.println("Sportscar can carry " + sportscar.passengers + " with a range of " + range2);

Add method // Add range to Vehicle. class Vehicle { int passengers; // number of passengers double FuelCapacity; // fuel capacity double kmperliter; // fuel consumption // Display the range. void range() { System.out.println("Range is " + FuelCapacity * kmperliter); } class AddMeth { public static void main(String args[]) { Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); // assign values to fields in minivan minivan.passengers = 7; minivan.FuelCapacity = 16; minivan.kmperliter = 21; // assign values to fields in sportscar sportscar.passengers = 2; sportscar.FuelCapacity = 14; sportscar.kmperliter = 12; System.out.print("Minivan can carry " + minivan.passengers + ". "); minivan.range(); // display range of minivan System.out.print("Sportscar can carry " + sportscar.passengers + ". "); sportscar.range(); // display range of sportscar.