תירגול 9 אובייקטים.

Slides:



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

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Reusable Classes.  Motivation: Write less code!
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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!
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSCI 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
יסודות מדעי המחשב – תרגול 6
Lecture 5:Interfaces and Abstract Classes
Andrew(amwallis) Classes!
Object Oriented Programming
PRG 420 NERD Marvelous Learning / prg420nerd.com
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Delegates and Events 14: Delegates and Events
ENEE150 Discussion 13 Section 0101 Adam Wang.
Creating Your OwnClasses
slides created by Marty Stepp
Namespaces, Scopes, Access privileges
Advanced Programming in Java
Objects First with Java Transaction List, error checking & aggregation
Still Aggregation and Composition
Understanding class definitions
Lecture 2: Implementing ArrayIntList reading:
Interface.
Building Java Programs
Building Java Programs
slides created by Marty Stepp and Ethan Apter
slides created by Ethan Apter
Interfaces and Constructors
String Methods: length substring
Building Java Programs
ITunes Lab Copyright © 2012 Pearson Education, Inc.
Lecture 7: Linked List Basics reading: 16.2
Lecture 2: Implementing ArrayIntList reading:
The Object-Oriented Thought Process Chapter 04
CSC 113: Computer programming II
slides created by Ethan Apter
CS2011 Introduction to Programming I Objects and Classes
slides created by Marty Stepp and Hélène Martin
Grouped Data Arrays, and Array Lists.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Understanding class definitions
COMPUTER 2430 Object Oriented Programming and Data Structures I
Instance Method – CSC142 Computer Science II
CIS 199 Final Review.
slides created by Marty Stepp
Creating and Modifying Text part 3
Building Java Programs
Barb Ericson Georgia Institute of Technology Oct 2005
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
Structures in c By Anand George.
slides created by Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
slides created by Ethan Apter
CSE 373: Data Structures and Algorithms
CS 240 – Advanced Programming Concepts
CHAPTER 3: Collections—Stacks
Presentation transcript:

תירגול 9 אובייקטים

היום בתרגול: המשך נושא מחלקות (classes) ואובייקטים (מופעים, objects). אובייקטים המכילים אובייקטים נוספים כשדות. מערכים של אובייקטים.

משחק מזל עם קוביה של 100 פאות ניתן להתייחס להגרלה של מספר שלם בתחום מ-1 עד n כאל זריקת קוביה בעלת n פאות. המשחק מתנהל בסיבובים. בכל סיבוב המחשב מגריל מספר בין 1 ל 3. אם המספר שהוגרל הוא 1, השחקן הראשון מאבד כמות נקודות אקראית בתחום 1 עד 100. אם המספר שהוגרל הוא 2, השחקן השני מאבד כמות נקודות אקראית בתחום 1 עד 100. אחרת כל אחד משני השחקנים מרוויח כמות נקודות אקראית בתחום 1 עד 100. (כל אחד יכול להרוויח כמות שונה). המנצח הוא השחקן שצבר יותר נקודות בתום 10 סיבובים.   האובייקטים המרכיבים את התוכנית הם שחקן, קוביה ומשחק. על סמך תיאור זה, מהן המחלקות? מהם השדות ומהן השיטות של כל מחלקה? מה מידת החשיפה שנרצה שתהיה לכל משתנה

int addPoints(int points) FIELDS Constructor String toString() int addPoints(int points) getters / setters

Constructor מהם הפרמטרים המועברים אליו? FIELDS Constructor מהם הפרמטרים המועברים אליו?

Student Class Define class Student containing student’s name and ID, read-only accessors and a String conversion method. Should the relevant fields be final? When do we need to qualify field access with this. ? What does each Student object contain exactly? Does it contain methods in addition to fields? How do methods know which object do they work with? What is the difference between this and any other object of type Student?

Student Class

Student Class

Course Class Define class Course that’s similar to student, maintaining course name and number. What if we forget to set some fields in constructor, but those fields are final?

Course Class

Course Class Design a way for each Course to keep track of registered students. Signatures: Student getStudent(long id) void addStudent(Student s) void print() We will use an array that grows with each add student. Need to reject adding students with ID already in the list.

Course Class

Course Class

Course Class

Questions Why did we print students in method print, and did not instead modify toString? Would this still work if students also kept list of courses they registered to, and also had a modified toString? How would we implement removal of students from a course? What if we wanted to keep students in sorted order in a list? How would we sort them, what would be the comparisons during sorting process?

Modify each Student similarly to Course, to keep the list of courses.

Course Class

Course Class