Download presentation
Presentation is loading. Please wait.
Published byKory Paul Sutton Modified over 9 years ago
1
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 + " "); }
2
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);}
3
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; }
4
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.
5
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; }
6
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?
7
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?
8
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); }
9
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:
10
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.
11
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); } }
12
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?
13
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.
14
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; }
15
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; }
16
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)
17
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.