Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java two neighbors 1. Initialization: 2.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.
Building Java Programs
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Return values.
Java Programming Abstract classes and Interfaces.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CS110 Programming Language I
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Lecture 15.1 Static Methods and Variables. © 2006 Pearson Addison-Wesley. All rights reserved Static Methods In Java it is possible to declare.
Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
1 Various Methods of Populating Arrays Randomly generated integers.
Building Java Programs
Computer Programming Lab 8.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Introduction to Java Programming Language May 2015 Kyung Eun Park COSC Introduction to Computer Science II.
Computer Programming Lab(4).
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
String and Scanner CS 21a: Introduction to Computing I First Semester,
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Building java programs, chapter 3 Parameters, Methods and Objects.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Creating and Using Class Methods. Definition Class Object.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Programming – Lecture 15 Going Beyond the ACM Library.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Advanced Programming in Java
Introduction to Classes and Methods
Java Lesson 36 Mr. Kalmes.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
The keyboard is the standard input device.
Presentation transcript:

Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance of string s.length() calls method defined in class Most methods in String return a new string

Examples of objects Scanner console = new Scanner (System.in); File f = new File(“hamlet.txt”); Scanner input = new Scanner (f); Scanner input = new Scanner (new File(“hamlet.txt”)); String s1 = new String (“hello”); Or String s1 = “hello”;

> import java.awt.Point; // auto-import > Point p = new Point(3,8); > System.out.println(p); java.awt.Point[x=3,y=8] > p.translate(-1,-2); > System.out.println(p); java.awt.Point[x=2,y=6]

Reference semantics int x = 3; //primitive type x 3//value semantics Point p = new Point(3,8); //object p  x 3 y 8//reference semantcis P refers to the object When working with objects, always working with reference to data rather than data itself

import java.awt.Point; public class PointExamp1 { public static void main(String[] args) { Point p1 = new Point(3,8); Point p2 = new Point(); Point p3 = p2; System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); p1.x = -1; p1.y = 1; p2.x = -2; p2.y = 2; p3.x = -3; p3.y = 3; System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); } > java PointExamp1 p1 = java.awt.Point[x=3,y=8] p2 = java.awt.Point[x=0,y=0] p3 = java.awt.Point[x=0,y=0] p1 = java.awt.Point[x=-1,y=1] p2 = java.awt.Point[x=-3,y=3] p3 = java.awt.Point[x=-3,y=3]

Multiple objects Point p1 = new Point(3,8); // separate instance Point p2 = new Point(); //separate instance Point p3 = p2; // points to p2

Objects as parameters to Methods public static void manipulate (Point p) { p.x = 99; p.y = 99; } Point p1 = new Point(3,3); manipulate(p1);

import java.awt.Point; public class PointExamp1 { public static void main(String[] args) { Point p1 = new Point(3,8); Point p2 = new Point(5,2); silly(p1); silly(p2); Point p3 = p1; silly(p3); System.out.println("p1 = " + p1); System.out.println("p2 = " + p2); System.out.println("p3 = " + p3); } public static void silly (Point p) { int temp = p.x; p.x = p.y; p.y = temp; }

Evaluate the following What are the x and y coordinates of the Points p1,p2, and p3 after the following? Point p1 = new Point(17,9); Point p2 = new Point(4,-1); Point p3 = p2; p1.translate(3,1); p2x = 50; p3.translate(-4,5)

public class StringComparision1 { public static void main(String[] args) { String name1 = "Bob"; String name2 = new String("Bob"); String name3 = "Bob"; // 1st case if (name1 == name2) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } // 2nd case if (name1 == name3) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); }

public class Temperature { public static void main(String[] args) { double tempf = 98.6; double tempc = 0.0; ftoc(tempf,tempc); System.out.println(“Body temp in C is: “ + tempc); } public static void ftoc(double tempf, double tempc) { tempc = (tempf – 32) * 5/ 9; }

Evaluate the following Math.abs(-1.6) Math.pow(6,2) Math.max(7,4) Math.min(-2,-5) Math.sqrt(64) Math.abs(2 + -4) Math.sqrt(16) * Math.max(Math.abs(- 5),Math.abs(-3))