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 + " "); }

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
CSCI 160 Midterm Review Rasanjalee DM.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Java Syntax Primitive data types Operators Control statements.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
UML Basics & Access Modifier
Multiple Choice Solutions True/False a c b e d   T F.
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
1 Exercise /* A lockbox can be open or closed. If closed, only a valid password will open the box. Once the box is open, the contents can be retrieved.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Best Practices. Contents Bad Practices Good Practices.
Agenda Object Oriented Programming Reading: Chapter 14.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Topic 4 Inheritance.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Builder Pattern. What’s Builder: TO find a solution to the telescoping constructor Problem: Too many parameters Solution: To get an abstract object, a.
Topics Instance variables, set and get methods Encapsulation
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Agenda Warmup AP Exam Review: Litvin A2
Objects in other classes:
Implementing Polymorphism
Start Simple: Class for rectangle: Class for bank account?
CLASS DEFINITION (> 1 CONSTRUCTOR)
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Object-Oriented Programming
CSE 1030: Implementing Mixing static and non-static features
COMPUTER 2430 Object Oriented Programming and Data Structures I
Agenda About Homework for BPJ lesson 36 About practice of last class
Basics of OOP A class is the blueprint of an object.
Object Oriented Programming Review
More on Creating Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

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 + " "); }

public class mainClass { public static void main(String[] args) { TestStatus obj = new TestStatus (3); System.out.println(obj.getval()); System.out.println(obj.getval2()); System.out.println(obj.c); //CANNOT DO System.out.println(obj.x); System.out.println(TestStatus.getval3()); System.out.println(TestStatus.z)} public class TestStatus { _______ int x = 1; _______ char c = 't'; _______ int z = 3; _______ TestStatus ( int k) { x = k; z --; } _______ int getval( ) { return(x); } _______ char getval2() { return (c); } _______ int getval3() { return (z);}

public Boolean ckmat(int[][] mat){ for (int i=0;i<mat.length;i++){ for (int j=0;j<mat[i].length;j++){ if ((j mat[i][j+1])) { return false; } if ((i mat[i+1][j])) { return false; } return true; }

Composition, Inheritance, and polymorphism Composition: defining a new class that is composed of other classes Inheritance: deriving a new class based on an existing class, with modifications or extensions.

StudentInfo Class public class StudentInfo { private String first; private String last; private String[] schedule; //Is this the best way to store class schedule? public StudentInfo(String f, String l, String[] sched) { first = f; last=l; schedule = sched; }

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 three fields: Dept, coursenum and section (we might even want course time and professor): public class Classes { public String dept; public int coursenum; Public int section; public Classes(String d, int cn, int sect) { dept = d; coursenum = cn; section = sect; } Can you now rewrite the student class by creating an array of Classes objects?

import javax.swing.JOptionPane; public class Student { public class StudentInfo { private String first; private String last; private Classes[] schedule; public StudentInfo(String f, String l) { first = f; last=l; schedule = getSchedule(); } public Classes[] getSchedule() { int num = Integer.parseInt(JOptionPane.showInputDialog("How many courses are you taking?")); Classes[] arr = new Classes[num]; for (int i = 0; i < num; i++) { String dept = JOptionPane.showInputDialog("Department?"); int coursenum = Integer.parseInt(JOptionPane.showInputDialog("Course Number?")); int sect = Integer.parseInt(JOptionPane.showInputDialog(“Section?")); arr[i] = new Classes(dept, coursenum, sect); } return(arr); } Printing out schedule?

Printing out student schedule? What is schedule an array of? What fields does that type have? public class Student { public class StudentInfo { private String first; private String last; private Classes[] schedule; … public void printSchedule() { System.out.format(“%s %s\n”,first,last); for (int i = 0; i < schedule.length; i++) { System.out.format(“%s %d %d \n”, schedule[i].dept, schedule[i].coursenum, schedule[i].section); } return(str); }

Objects in other classes: Create a class for a card (including constructor): public class Card { public int num; // why are these public? public String suit; public Card(int n, String s) { num = n; suit = s; } } Now Create a class for a deck of card objects:

public class Deck { public Card[] puppies; // why is this public? public Deck() { Card[] arr = new Card[52]; String[] suits = {"Club","Spade","Diamond","Heart"}; int index = 0; for (String s: suits) { for (int i = 1; i <= 13; i++) { arr[index] = new Card(i,s); index++; } } } public String toString() { String str=""; for (Card i: puppies) { str += i.num + i.suit + " "; } return(str); } } So far so good. What if we wanted to create a game that has a deckofcards field that is a Deck object. The game generates a random card from the deck and prints it out.

Create a game that has a deckofcards field that is a Deck object. The game generates and prints out a random card. import java.util.Random; public class CardGame { private Deck deckofcards; public CardGame() { deckofcards = new Deck(); } public void printRandomCardNum() { Random r= new Random(); int x = r.nextInt(deckofcards.puppies.length); System.out.println(deckofcards.puppies[x].num); System.out.println(deckofcards.puppies[x].suit); } }

this -a reference to the current object - the object whose method or constructor is being called. What if we had: public class Card { public int num; // why are these public? public String suit; public Card(int num, String suit) { num = num; suit = suit; } How can you tell which num is which, and which suit is which?

this public class Card { public int num; // why are these public? public String suit; public Card(int num, String suit) { this.num = num; this.suit = suit; } Now we know that this.num refers to the field of the class Card, and num refers to the input parameter.

this – with constructors Sometimes a constructor initializes a lot of fields. With different versions of the constructor, coding all the initializations gets tedious. We could use this to call the constructor, e.g., public class Rectangle{ private int x; private int y; private int width; private int height; public Rectangle() { this(0,0,1,1); } public Rectangle(int width, int height) { this(0,0,width,height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }

public class Neighbor { int housenum; String streetname; Neighbor leftneighbor; Neighbor rightneighbor; public Neighbor(int housenum, String streetname) { //constructor 1 this(housenum,streetname,null,null); } public Neighbor(int housenum, String streetname, Neighbor leftneighbor) { // constructor 2 this(housenum,streetname,leftneighbor,null); } public Neighbor(int housenum, String streetname, Neighbor leftneighbor, Neighbor rightneighbor) { //constructor 3 this.housenum = housenum; this.streetname = streetname; this.leftneighbor = leftneighbor; this.rightneighbor = rightneighbor; } public Neighbor makeright() { // If we’re making a right neighbor, then what is that’s left neighbor? Neighbor right = new Neighbor(housenum + 2, streetname, this, null); return right; } public Neighbor makeleft() { Neighbor left = new Neighbor(housenum - 2, streetname, null, this); return left; }

Try: Write a class for a parent (maybe gender, name,phonenumber?) if you are a parent, you must have a child Write a class for a child (maybe gender, name, age?) If you are a child, you must have a parent Now in the parent class, write a method that creates a child (use this)

public class Parent { int phonenum; String firstname; String lastname; char gender; Child child; public Parent(int phonenum, String first, String last, char gender ) { this.phonenum = phonenum; this.firstname = first; this.lastname = last; this.gender = gender; this.child = null; } public void makeChild() { Random r = new Random(); int x = r.nextInt(2); if (x == 0) { String first = JOptionPane.showInputDialog("What is the boy's name?"); child = new Child(first,lastname,"boy", 0, this); } else { String first = JOptionPane.showInputDialog("What is the girl's name?"); child = new Child(first,lastname,"girl", 0, this); } public class Child { String first; String last; String gender; int age; Parent parent; public Child(String f, String l, String gen, int age, Parent p) { this.first = f; this.last = l; this.gender = gen; this.age = age; this.parent = p; }