Instance Method Development Demo

Similar documents
Practice Problems: Instance methods

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Software and Programming 1

Software and Programming 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Software and Programming 1

Chapter 4 Defining Classes I

Objects as a programming concept

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

CS 302 Week 9. Jim Williams

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

PASS4TEST IT 인증시험덤프전문사이트

CS212 Midterm. 1. Read the following code fragments and answer the questions.

This exam is open book. Each question is worth 3 points.

( &% class MyClass { }

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CPSC 219 Extra review and solutions

CSCI 212 Practice Final Exam Summer Instructor: Krishna Mahavadi

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

AP Computer Science A Summer Assignment 2017

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

University of Palestine. Mid Exam Total Grade: 100

What is Inheritance?

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Solutions to Sample JAC444 Midterm Test

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

CS1083 Week 2: Arrays, ArrayList

AP CS Unit 4: Classes and Objects Programs

Slide 1 CS 170 Java Programming 1

Final Exam Practice. Partial credit will be awarded.

A First Object. We still have another problem. How can we actually make use of the class s data?

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

In Java there are three types of data values:

CH. 2 OBJECT-ORIENTED PROGRAMMING

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Chapter 6 Lab Classes and Objects

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

COMP102: Test. 31 August, 2005

Computer Science II (20082) Week 1: Review and Inheritance

Java for Non Majors Spring 2018

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

ESC101 : Fundamental of Computing

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS Computers & Programming I Review_01 Dr. H. Assadipour

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

Introduction to Java. CSC212 Lecture 6 D. Thiebaut, Fall 2014

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Last Name: Circle One: OCW Non-OCW

I. True/False: (2 points each)

CSC142, Computer Science II, Project 5 (2 nd submission)

EnclosingClass.StaticClass staticclass = new EnclosingClass.StaticClass(); System.out.println(staticClass.getName());

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CSCI 161 Introduction to Computer Science

CSCI 355 Lab #2 Spring 2007

Lecture 12: Classes II

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

CSCI 111 Midterm Spring 2014

Review Ch 5 static Multiple Choice Test Creating Class Methods

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Java exercises January François de Coligny, Nicolas Beudez

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

UNDERSTANDING CLASS DEFINITIONS CITS1001

Assignment 1 due Monday at 11:59pm

public static boolean isoutside(int min, int max, int value)

Spring % of course grade

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

APCS Semester #1 Final Exam Practice Problems

Branching. Chapter 5 11/14/16 & 11/15/16

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

CSCI 355 LAB #2 Spring 2004

Final Examination Semester 3 / Year 2010

Chapter 10. Further Abstraction Techniques

Conditional Execution

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CSIS 10B Lab 2 Bags and Stacks

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Classes and Methods: Classes

Informatik II (D-ITET) Tutorial 6

Chapter 6 Class and Method

Two hours QUESTION PAPER MUST NOT BE REMOVED FROM THE EXAM ROOM UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

CS 113 MIDTERM EXAM 2 SPRING 2013

CSCI 1101 Winter 2017 Laboratory No Submission deadline is p.m. (5 minutes to midnight) on Saturday, February 4th, 2017.

Solutions to the 2005 exam

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CIS 110 Introduction to Computer Programming. 7 May 2012 Final Exam

Transcription:

Instance Method Development Demo Write a class Person with a constructor that accepts a name and an age as its argument. These values should be stored in the private attributes name and age. Then, write necessary accessors and mutators to support the given class Registra as shown in the below. The desired execution is also presented. Explain when two person records can be identified as the same and when they have the same information (but are different records). Without this explanation, you will lose half of the score of this question. (Hint: for the development to support lines 16, 21, 26, 31, and 34, please refer to slide 21 in the ppt file under the link difference from the class website. It is also inside the zip file of project materials.) 1 public class Registra{ 2 public static void main(string args[]){ 3 Person a = new Person("Smith", 19); 4 Person [] csc240 = {new Person("Kathy", 18), 5 new Person("Martin", 21), new Person("Smith", 19), a}; 6 System.out.println("The person newly registed this class: "+a); 7 System.out.println("--------join in the class-----------"); 8 for(int i = 0; i<csc240.length; i++){ 9 System.out.println("The person already in class: "+csc240[i]); 10 11 if(a==csc240[i]) 12 System.out.println("They are the same person object."); 13 else 14 System.out.println("They are different people."); 15 16 if(a.has_same_name(csc240[i])) 17 System.out.println("They have the same name."); 18 else 19 System.out.println("They have different names."); 20 21 if(a.has_same_age(csc240[i])) 22 System.out.println("They have the same age."); 23 else 24 System.out.println("They have different ages."); 25 26 if(a.has_same_name(csc240[i]) && a.has_same_age(csc240[i])) 27 System.out.println("Personal information is the same (equal)."); 28 else 29 System.out.println("They are different people."); 30 31 if(a.is_younger_than(csc240[i])) 32 System.out.println("The new person is younger than student "+ i 33 + "in class (" + csc240[i]); 34 else if (a.is_older_than(csc240[i])) 35 System.out.println("The new person is older than student "+ i 36 + "in class (" + csc240[i]); 37 else 38 System.out.println("The new person has the same age as student "+ i 39 + "in class (" + csc240[i]); 40 System.out.println("----- end of the check of one person -----\n"); 41 } 42 } 43 }

Solution (including each step): Step 1: Ensure the appropriate compiler and development environment. Can you run the program and get the result like this? Step 2: Add the first a few lines in application class Registra and see if this additional part can be supported. For instance, if we have the class Person like the follows,

the application lacks the support and will give the errors: Q&A1: What to do to support new operation? constructor. So we have the following program implemented in the support class Person: newly added! The result is:

1) No syntax error and 2) execution is good and does not throw any exception. Q&A2: What is the result, and can this be displayed? Step 3: Add two more lines and see if they can be supported: tostring is needed in class Person to avoid displaying a strange memory address. Check if this is enough as the desired support:

Object content, from its attributes Step 4: Continue to add more lines in application class Registra: No need for extra support.

Q&A3: How can I see two identical Smith in the check? -- One is initiated with the same content and the other is the exact same object (i.e., object a ). Step 5: How about adding more lines in the application class? Q&A4: What support is needed from the class of Person? 1) method has_same_name, and 2) the relevant accessors as the support of such a supporting method. Newly added! Cannot always be true! So, Q&A5: How to get the name of host object, and how to get the name of the right object? accessor, i.e., getname. Refer to the ppt document under the link ``difference from the class website.

Q&A6: How to compare these two names? String equals. Newly added! The execution result can be confirmed as: Q&A7: Why the last two Smith objects have different results: one says that they are the same and the other says that they are different? See the object new operation, as we explained in Q&A3. Step 6: Now the entire application class does not have technical obstacle.

Newly added. Step 7: Test the execution of this completed version and compare with the desired one in the assignment sheet.