Announcements Lab 5 due Wednesday at noon.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Written by: Dr. JJ Shepherd
Catie Welsh March 2,  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
COMP More About Classes Yi Hong May 22, 2015.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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!
Classes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
CSC 212 Object-Oriented Programming and Java Part 2.
February 28, 2013 COMP Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Written by: Dr. JJ Shepherd
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
A High Flying Overview CS139 – Fall 2006 How far we have come.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.
Defining Your Own Classes II
Information and Computer Sciences University of Hawaii, Manoa
Objects Real and Java COMP T1 3
GC211 Data structure Lecture 3 Sara Alhajjam.
Java Language Basics.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Java Course Review.
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
COMP Objects and References
COMP 110 Review for midterm exam
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
CS139 – Fall 2010 How far we have come
Classes Variables That Are Not of a Built-in Type Are Objects
Encapsulation and Constructors
Building Java Programs
More on Classes and Objects
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Defining Classes and Methods
Building Java Programs
Classes CS 21a: Introduction to Computing I
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Announcements Assignment 2 and Lab 4 due Wednesday.
Barb Ericson Georgia Institute of Technology Oct 2005
Review of Previous Lesson
Building Java Programs
Class rational part2.
Control Structure.
Review for Midterm 3.
ITM 352 Functions.
Classes and Objects Object Creation
Chapter 7 Objects and Classes
Presentation transcript:

Announcements Lab 5 due Wednesday at noon

Questions? Classes, Instance Vars, & Methods visibility accessors/mutators parameters interface versus implementation

Today in COMP 110 Review Some common mistakes (recent) Programming Demo Objects & References

Review Started with simple arithmetic Needed choice Needed repetition variables and expressions Needed choice Perform different actions based on different inputs if statements switch statements Needed repetition Can do known number of iterations without loops Just copy and paste code (Triangle) For variable number of iterations: while, do-while, for loops

Review Duplicating code is hazardous prone to mistakes, tedious to change (Sandwiches) consolidate replicated code into a method may want the method to take in a value (parameter) may want the method to return a value (return type) Duplicating data is also hazardous finite amount must be known at compile-time run out of variable names! code becomes too long and not readable Use classes instead

Programming Constructs Variables/Expressions Conditionals Loops Methods Classes (also enumerations) More to come...

If-Statement Example What is the output? int x = 7; int y = 5; if(y > x) x = x + y; System.out.println("y > x"); System.out.println("y <= x"); Output y > x y <= x

If-Statement Remember to include curly braces for if-statement bodies that include multiple statements int x = 7; int y = 5; if(y > x) { x = x + y; System.out.println("y > x"); } System.out.println("y <= x");

Local Variables What is the output of greet()? Output hello public class Example { private String str = “hello”; public void foo() { String str = “goodbye”; } public void greet() { foo(); System.out.println(str); Output hello

Local Variables What is the output of greet()? Output goodbye public class Example { private String str = “hello”; public void foo() { str = “goodbye”; } public void greet() { foo(); System.out.println(str); Output goodbye 10

Accessors & Mutators public class Example { private double data; //private, can not be accessed directly //from OUTSIDE the class public double getData() { //this is an accessor return data; //this method is the ONLY way to access } //“data” public void setData(double newData) { //this is a mutator data = newData; //this method is the ONLY way to } //change “data” }

Accessors & Mutators public class ExampleTest { public static void main(String[] args) { Example e = new Example(); e.data = 6.5; //not allowed, data is private e.setData(6.5); //ok, only way to set data double d = e.getData(); //ok, only way to get data }

Programming Demo Grading Program Functionality Two quizzes – 10 points each A midterm and final – 100 points each Final Exam – 50% Midterm – 25% Quizzes – 25% Functionality Read in a students score and display record

Programming Demo Approach Instance variable for each score Method to read input Method to display student record Including total score and final grade(A-F) Use two helper methods One to calculate total score Other to get letter grade

Programming Demo Programming

Variables of a Primitive Type Variables of primitive type hold a value int a = 6; double d = 6.55; boolean b = a > d; We can say The value of a is 6 The value of d is 6.55 The value of b is false

Variables of a Class Type Classes can have multiple data members public class Student { public String name; public int year; public double GPA; public String major; //… } What is the “value” of a variable of a class type? Student jack = new Student();

Variables of a Class Type The value of a variable of a class type is a memory address The address of the object it refers to The address to this other location is called a reference to the object Class types are also called reference types

Example: Books public class Book { private String name; private int page; public void setPage(int page) { this.page = page; } public void setName(String name) { this.name = name;

Example: Books Assume we have a class named Book vs. Book jacksBook = new Book(); Book samsBook = new Book(); vs. Book samsBook = jacksBook; //samsBook now refers to the same object as jacksBook

Objects in Memory Memory jacksBook samsBook 2078 1056 2078 ? 2078 ? ? Book jacksBook; Book samsBook; jacksBook = new Book(); jacksBook.setName("Java"); samsBook = new Book(); samsBook.setName("Java"); jacksBook.setPage(137); samsBook.setPage(253); samsBook = jacksBook; samsBook.setPage(509); jacksBook is now on p. 509! jacksBook samsBook 2078 1056 2078 ? 2078 ? ? 2078 ? 1056 2078 Java ? 137 ? ? Java Java ? Java 253 137 Java 253 509

Remember Variables of a class type contain memory addresses NOT objects themselves

== Operator on Objects The == operator checks whether the values of two variables are the same The value of class variable is a memory address When using the == operator to compare two objects, you are checking whether they have the same address in memory

== vs. equals() for Strings Explained String is a class type What happens when you have String s1 = new String("Hello"); String s2 = new String("Hello"); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses!

== vs. equals() for Strings explained What happens when you have String s1 = new String("Hello"); String s2 = new String("Hello"); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s .equals() method checks if all the characters in the two Strings are the same

Defining an equals Method Every class has a default .equals() method if it is not explicitly written Does not necessarily do what you want You decide what it means for two objects of a specific class type to be considered equal Perhaps books are equal if the names and page numbers are equal Perhaps only if the names are equal Put this logic inside .equals() method

Writing the .equals() Method public class Book { private String name; private int page; //… public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); }

Parameters of a Primitive Type public void increaseNum(int num) { num++; } public void foo() { int x = 5; increaseNum(x); System.out.println(x); What is the output? 5

Parameters of a Class Type public void changeBook(Book book) { book = new Book("Biology"); } public void foo() { Book jacksBook = new Book("Java"); changeBook(jacksBook); System.out.println(jacksBook.getName()); What is the output? Java

Parameters of a Class Type public void changeBook(Book book) { book.setName("Biology"); } public void foo() { Book jacksBook = new Book("Java"); changeBook(jacksBook); System.out.println(jacksBook.getName()); What is the output? Biology 30

Wednesday Constructors