Start Simple: Class for rectangle: Class for bank account?

Slides:



Advertisements
Similar presentations
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 6. Overloading Methods and Constructors  Two or more methods in a class may have the same name as long as their parameter lists are different.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Chapter 7 – Arrays.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
(Quickly) More on Null references: A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COT int[] x = {3,7,2,4,1}; int[] y = {5,8,6,9}; x = y; x[2] = 0; for (int k: y) { System.out.print(k + " "); }
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Array in C# Array in C# RIHS Arshad Khan
Topic: Classes and Objects
Lecture 5 of Computer Science II
Object Oriented Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Chapter 8 – Arrays and Array Lists
Foundations of Programming: Arrays
Strings A string is a sequence of characters treated as a group
Writing Methods AP Computer Science A.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Arrays 6-Dec-18.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Today’s topics UML Diagramming review of terms
Arrays ICS2O.
Arrays and Array Lists CS 21a.
slides created by Ethan Apter
Multidimensional array
Lecture 12: 2D Arrays AP Computer Science Principles
Arrays an array of 5 ints is filled with 3,2,4,1,7
JAVA CLASSES.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Arrays 2-May-19.
CS31 Discussion 1D Winter19: week 4
Review for Midterm 3.
Classes and Objects Object Creation
Object-Oriented Programming and class Design
CMSC 202 Constructors Version 9/10.
Object-Oriented Programming and class Design
Presentation transcript:

Start Simple: Class for rectangle: Class for bank account? What fields? What methods? Setting public and private? Class for bank account? Fields? Methods? Public and private?

public class Rectangle { private double length; private double width; private double area; private String fillcolor; Rectangle() { length = 1.0; width = 1.0; setArea(); fillcolor = "white"; } Rectangle(double l, double w) { length = l; width = w; Rectangle(String s) { fillcolor = s; private void setArea(){ area = length * width; public void setlen(double l) { public void setwid(double w) { public String toString(){ String s="Rect: "+(double)((int)(length*100))/100+", "+(double)((int)(width*100))/100+" Area: "+(double)((int)(area*100))/100; return s;

public class BankAccount { private String name; private double balance; private double interest; public BankAccount(String n){ name = n; balance = 0; setInterest(); } public BankAccount(String n, double B) { balance = B; public void setInterest() { if (balance > 100000.0) { interest = .25; else if (balance > 10000.0) { interest = .15; else if (balance > 500.0) { interest = .075; else { interest = 0.0; public void deposit(double m) { balance += m; public void withdraw(double m) { balance -= m; public void calcInterest() { balance += (balance * interest); public String toString(){ String s = name+": "+(double)((int)(balance*100))/100; return s;

2-D Arrays Make a one-dimensional array of integers: int[] arr1d = {3,2,4,1,5}; How many elements in the array? (arr1d.length?) How do you access the element at index 3? Now make an array that consists of arrays of ints: int[][] arr2d = {{1,2,3,4},{5,6,7,8},{9,10,11,12}} ; Or (if we don’t know the values: int[][] arr2d = new int[3][4]; Can you make an array of arrays of arrays? (a 3- Dimensional array?)

2D Arrays one-dimensional arrays to model linear collections of elements. two-dimensional arrays represent a matrix or a table Example:

Two-dimensional Array Illustration arr.length? arr[0].length? What data type is arr[2] ?

Matrices: Making Arrays of Arrays 1. double[][] mat = new double[5][]; What have I just made? an array of 5 addresses (that will eventually point to arrays of doubles). If I can’t do this: 2. double[][] mat = new double [][]? Why can I do #1?

To create an array of arrays: You can do: double[][] mat = {{3.2,4.1,2.5},{7.1,8.2,9.3}}; Or double[][] mat = new double[3][]; mat[1] = new double[] {3.1,2.4}; double[] arr = {7.2,3.1,2.4}; mat[2] = arr; double[][] mat; mat = new double[][] {{3.2,4.1,2.5},{7.1,8.2,9.3}};

What is Sudoku? Every row must have each number between 1 and 9 only once Every column must have each number between 1 and 9 only once Every matrix of 9 must have each number between 1 and 9 only once Can you write a method that checks? 1 for rows 1 for columns 1 for matrices (3x3) How do we check that each number occurs only once? Now- one for whole board?

Accessors and Mutators Accessors (e.g., getField) public instance methods that access private data may return different forms of the data simple convention: getSomeProperty() Mutators (e.g., setField) public instance methods that change private data may change more than one private data element simple convention: setSomeProperty(x) Why are these good ideas?

Static Variables and Methods Static variables and methods are INDEPENDENT of individual objects made from a class Everything else is specific to the particular object being created from the class. Static variables (fields) belong to a Class similar to global variables //avoid global variables!! Use mostly for constants easy to create bugs and undefined behavior Beginners try to make everything STATIC You don’t want to do that. Most of the time, we don’t want our fields (or our methods) to be static.

Static methods belong to a Class Do not have access to object fields cannot call nonstatic methods (because they use object variables) often have good uses as simple functions formulas common to all objects in a Class are often written in a static method Utility classes – think of the java.util… that we’ve imported – has methods we want to be able to use without having the methods attached to an object.

Versus Instance Variables, and Methods (variables and methods without "static") Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

Static fields and methods public class Creature { private int currentmood = 2; private String name = "Noname"; private static int creaturecount = 0; private String[] moods = {"massively depressed","bored stiff","marginally happy", "ecstatic"}; public Creature() { creaturecount++; } public Creature(int mood) { currentmood = mood; public Creature(String Creaturename) { name = Creaturename; public Creature(int mood, String Creaturename) { public String getMood() { return (name +"'s current mood is "+moods[currentmood]); public static String NumberinHerd(){ return ("The current number of creatures is " + creaturecount); public class mainclass { public static void main(String[] args){ Creature Fluffy = new Creature (3); System.out.println(Creature.NumberinHerd()); System.out.println(Fluffy.getMood()); Creature Bob = new Creature(0, "Bob"); System.out.println(Bob.getMood()); Creature Killer = new Creature("Killer"); System.out.println(Killer.getMood()); } Static fields and methods

The null Value Example: If a data field of a reference type does not reference any object, the data field holds a special literal value, null. (null reference value) Example: Circle circle1 = new Circle(5.0); Circle circle2 = null; System.out.println(circle1.getrad()); // what happens here? System.out.println(circle2.perimeter()); // here? circle2 = circle1; // what happens here? circle2.setrad(4.2); System.out.println(circle1.getrad()); //

One more thing on Null references How do you get rid of an object? In a game, what if a player dies? You might want to clear the player object out of memory, but still retain the ability for the rest of the game to detect that the player is “dead” You’d do this by setting the player object to null Then you can check: if (players[x] != null)

Student Class String str = ""; str += first + "\t"+ last + "\n"; public class StudentInfo { private String first; private String last; private String[] schedule; public StudentInfo(String f, String l, String[] sched) { first = f; last=l; schedule = sched; } public String toString() { String str = ""; str += first + "\t"+ last + "\n"; for (int i = 0; i < schedule.length; i++) { str += schedule[i]+ "\t"; return(str); Note the \t and \n - “escape sequences” \t adds a tab \n adds a new line (makes the printout go to the next line.

Alternative Student Class Instead of making the schedule array be an array of Strings, make it be an array of objects of type Classes. The Classes type has two fields: Dept and coursenum: public class Course { public String dept; public int coursenum; public Course(String d, int cn) { dept = d; coursenum = cn; } Can we now rewrite the student class by creating an array of Classes objects?

public Course(String d, int cn) { dept = d; coursenum = cn; } public class Student { private String first; private String last; private Course[] schedule; public StudentInfo(String f, String l) { first = f; last=l; schedule = getSchedule(); } public Course[] getSchedule() { int num = Integer.parseInt(JOptionPane.showInputDialog("How many courses are you taking?")); Course[] arr = new Course[num]; for (int i = 0; i < num; i++) { String dept = JOptionPane.showInputDialog("Department?"); int coursenum = Integer.parseInt(JOptionPane.showInputDialog("Course Number?")); arr[i] = new Course(dept, coursenum); return(arr); And now toString? public class Course { public String dept; public int coursenum; public Course(String d, int cn) { dept = d; coursenum = cn; } //why are the fields public? Should I have done something different here?

My toString() What type is schedule an array of? public String toString() { String str = ""; str += first + "\t"+ last + "\n"; for (int i = 0; i < schedule.length; i++) { str += schedule[i].dept + " " + schedule[i].coursenum + "\n"; } return(str); What type is schedule an array of? What fields does that type have?