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

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

Recommended Group Brainstorm (NO computers during this time)

CSCI 1301: Introduction to Computing and Programming

EE 422C HW 6 Multithreaded Programming

Part 1: Group Brainstorm (NO computers during this time) Part 2: Submit Individual Brainstorm (You can now use a computer)

CPSC 233: Assignment 4 (Due March 26 at 4 PM)

Chapter 4 Defining Classes I

CSCI 1301: Introduction to Computing and Programming Spring 2018 Project 3: Hangman 2.0 (A Word Guessing Game)

PA3: Violet's Vending Venture (Version 3.0)

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Programming II (CS300)

CHAPTER 7 OBJECTS AND CLASSES

Introduction to Programming Using Java (98-388)

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

System.out.print(); Scanner.nextLine(); String.compareTo();

Classes Classes 2 / 36

// 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 }

Principles of Object Oriented Programming. Lecture 4

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

CS 302 Week 9. Jim Williams

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Classes Classes 2 / 35

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CHAPTER 7 OBJECTS AND CLASSES

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Chapter 12: How to Create and Use Classes

Programming Exercise 14: Inheritance and Polymorphism

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Today s Agenda. Quick Review

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

Tips from the experts: How to waste a lot of time on this assignment

CSCE 156 Computer Science II

CSE115 Lab 4 Fall 2016

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Tips from the experts: How to waste a lot of time on this assignment

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Chapter 6 Introduction to Defining Classes

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Programming II (CS300)

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Introduction to Classes and Objects

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Recitation 3 Class and Objects

Framework Fundamentals

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.

EECS168 Exam 3 Review

Data Structures (list, dictionary, tuples, sets, strings)

CS18000: Programming I

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming in Java

MIDTERM REVIEW. midterminformation.htm

Java and OOP. Part 2 Classes and objects

CSCI 161 Introduction to Computer Science

Tips from the experts: How to waste a lot of time on this assignment

Function Overloading

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

CompuScholar, Inc. 9th - 12th grades

Ticket Machine Project(s)

Week 3 Classes and Objects

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

ITI Introduction to Computing II

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

ECE 122. Engineering Problem Solving with Java

Assignment 19 Deadline: Nov pm COSC211 CRN15376 Session 15 (Nov. 7)

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

CS313D: ADVANCED PROGRAMMING LANGUAGE

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Objects as a programming concept

OOP- 5 Stacks Individual Assignment 35 Points

PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Software and Programming 1

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

Defining Classes and Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNDERSTANDING CLASS DEFINITIONS CITS1001

Java Foundations Certified Junior Associate

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Chapter 4: Writing Classes

Chapter 6 Lab Classes and Objects

Instance Method Development Demo

ITI Introduction to Computing II

COMP 401 Spring 2013 Midterm 1

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

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Understanding class definitions

Short Notes of CS201

Overview of Eclipse Lectures. Module Road Map

Transcription:

Introduction This lab introduces you to additional concepts of Object Oriented Programming (OOP), arguably the dominant programming paradigm in use today. In the paradigm, a program consists of component parts (objects) that are independent of each other and that interact in order to achieve a desired result. At a very basic level, an object oriented program consists of the following types of things: Classes: Objects: The template or blueprint from which objects are created. The instantiation of a class. Once a class (the template) has been defined, many instances of it can be created. Methods: The named procedures or functions defined for an object. A method accepts input arguments and contains statements that are executed when the method is invoked. Some methods return values. Instance Variables: Variables that are initialized when an object is created. Each instance of a class can have distinct values for these variables. In this lab, you will practice working with these fundamentals. A skeleton of code has been written for you defining the basic class and method structure that you are to use. You will then add code to the existing skeleton methods so that the classes function as intended. You are then to use a testing program illustrate how the component parts of what you have created interact. Lab Objectives By the end of the lab, you should be able to create classes utilizing: access modifiers; instance variables (also called fields); methods which return values and void methods (which do not return any value); methods calling other methods; method overloading; accessor and mutator methods; The equals() method; The tostring()method. What to Submit The files Main.java, Person.java and House.java should be submitted to elc for grading.

Instructions Three partially implemented files can be found on the course website. You should download these files and put them into a new project in your workspace in eclipse. The file House.java contains a partial definition for a class representing a house that is bought and sold on a real estate marketplace. It will hold data such as whether or not is for sale, what its price is, and its area and volume. The file Person.java contains a partial definition for a class representing a person in a real estate market. Functionally, the Person class exists to encapsulate data about users of our program, such as whether or not they own a home, how much money they have etc. Save each to your computer and study them to see what methods they contain. Read and understand the method specifications of each. Then complete both the House class and Person class as described below. Note that you should test your methods in Main.java. 1. You must include a comment stating your name, the date, program purpose, and containing a statement of academic honesty. When writing, you must also abide by the Java formatting and naming conventions. 2. The House class is partially defined with an enum Color already defined for you. Fully define the House class by following the instructions below: a. Four private doubles instance variables price, width, length, height have been declared for you. Yes, we are assuming that the house is a 3-D rectangular box for simplicity. Next, we ve also declared a private Color instance variable called color and a private boolean instance variable named forsale, which represent the color of the house and whether or not the house is for sale respectively. b. We will use two different constructors for House by utilizing method overloading. The default constructor (no parameters) will generate an instance of House with random values for its instance variables. The bounds of what these should be are defined by the method specification (the multiline comment above the method header). The default constructor has already been written for you. Hint: Note how we accessed and assigned instance variables without using the this keyword in the default constructor. We can do this because there are no parameters or local variables with the same name. c. The second constructor is much simpler to implement than the first. You merely need to match the parameter and instance variables. There is a twist however: the second constructor s parameters share the same name as the object s instance variables. We must therefore use the this keyword to access our object s instances variables. For example, to access the color instance variable of the House object, we write this.color. Assign the instance variables to the matching parameters.

Also note that when a house is created it is for sale. Therefore the forsale instance variable should be set to true at initialization. d. The tostring() method has already been defined for you. Notice how this method calls the object s other methods. If those are not complete, this method will not work! e. Fill in the code for the equals() method. A house is equal to another if ALL of their instance variables are equal. For this method, check all instance variables other than forsale. Two houses can be the same even if one is for sale and the other isn t. f. Next we will define the area() method, which returns the area of our house. The area is simply the width instance variable multiplied by the length instance variable. g. Define the volume() method. The volume is equal to area() multiplied by the height. h. Next we will define our setter or mutator methods. These methods modify/mutate/set the instance variables of House objects. Each of their signatures begins with set, and work similarly to our second constructor. The instance variable being mutated will be assigned to the parameter of each method. i. Finally we will create our accessor methods, also called getters. All these methods do is return the instance variable given away by their name. They are always prefixed by get- or is-. For example getprice() returns the price instance variable of the House object. 3. The next step is to test the House class that you have just defined. Open Main.java. You will notice a large area of commented out code. This is code for a small real estate application that will use your classes as building blocks to run. We will uncomment this after we finish the Person class. At the beginning of the main method write: House h = new House(); Now add a breakpoint right after, then run your program in debug mode and observe the values in your instance variables. Now try printing your house to the console: System.out.println(h.toString()); Compare the output on the console to the instance variables you observed. If need be fix your getter methods to get the right output. Continue this pattern of testing each of your class s methods by using Eclipse s debugging, and once you are confident they work properly, move on to step 4. 4. Start writing the Person class, following the instructions detailed below: a. There are four private instance variables: String name, int age,double cash and House house. The instance variable house represents the person s home, assuming they have

one. If they do not, house will be a null reference. Note that you should not declare any other instance variables other than these four. b. Define the default constructor (no parameters) so that the person who is instantiate has the name John L., has an age of 21 years old, has a single penny of cash and no home. Note that you must set the house instance variable to null. c. A second constructor is overloaded with several different sets of parameters. Fill in the code for the constructor that does not contain the house parameter. The constructor should assign its instance variables to the matching parameters, and set the house instance variable to null. d. Fill in the code for the third constructer, which does contain a house parameter. This constructor must in addition to matching the correct instance variable to the correct parameters, must mutate the house object passed in by the parameter so that it is no longer for sale. e. Fill in the code for method tostring(), which should return a string containing values for the name, age, cash, and house (if the user has one). The returned string value should be formatted as below: Name: name Age: age Cash: $cash Home:\n house.tostring() // do not print this line if // house is null f. Fill in the code for the accessor methods: getname(), getage(), getcash(), and gethouse(). g. The last of the non-mutating methods we need to complete is ownsahouse(). This will return true if the user has a home. This method is not very complex, but it makes our code more human-readable when it is used by other methods we will create later in this lab.. Stop here and test that your Person class is currently working by setting breakpoints in its methods and testing it in the Main class. This will also help you observe the flow of an object-oriented program. As is, the Person class does not have any methods that mutate its instance variables (and thereby the Person object), so you should ensure everything is correct now before the class becomes more complex. 6. Complete the Person class following the instructions detailed below: a. Complete the addcash() method. Simply add the amount parameter to the cash instance variable. b. Complete the sellhome() method. The Person instance will have the price of the house added to his cash, and the house will be for sale again and the program should print name has sold their house! However, if this user does not have a house, the program should print name has no house to sell. After the house has been sold back to the market, the Person s house instance variable is set to null.

c. Fill in the code for the buyhouse() method. Very specific requirements have to be met for the person to purchase a home successfully: i. They must not already own a home ii. They must be able to afford the home iii. The home must actually be for sale If the person meets these requirements, the person is subsequently charged the price of the house (their cash decreases by that price) and the house instance variable is assigned to the newly purchased house. Ensure you set house s instance variable forsale to false. d. Test your methods using the Main class once again. 7. Once you are confident each of these classes functions as expected, uncomment the code in the main method, and run the application! The takeaway from this lab is that the great strength of Object Oriented Programming is that it enables two programmers to cooperate easily. This application was written and is now able to run without the programmer knowing your specific implementation of each of these classes and methods. With Object Oriented programming, teams can work together on projects without every programmer worrying about implementation of each class. With the complexity today s applications, this advantage is huge. elc Submission and Grading After you have completed and thoroughly tested your programs, upload House.java, and Person.java to elc to receive credit. Always double check that your submission was successful on elc! The lab will be graded according to the following guidelines. A score between 0 and 100 will be assigned. If the source file(s) are not submitted before the specified deadline s late period ends (48 hours after the deadline) or if they do not compile then a grade of 0 will be assigned. Late penalties and penalties for unexcused lab absences will be deducted per the syllabus. If the required comment for all labs describing the program and the academic honesty statement is not included at the top of the file, then points will be deducted. Note: this required comment can be found in Lab 02. Examples Your program should work correctly and follow the examples below. Each example is a separate run of a correctly working program. Please note that some long lines of output are wrapped around multiple lines in this document, and it is okay if your output displays them on a single line. ---------------------------------------------------------------------

Enter your name: John Smith Enter your age (in years): 0 Enter the amount of money you have: 7000 You are: Name: John Smith Age: 0 years old Cash: $ 7,000.00 4 Name: John Smith Age: 0 years old Cash: $ 7,000.00 1 House 1 Dimensions: 184.490 x 10.220 x 6.10 meters Price: $47,110.37 House 2 Dimensions: 8.910 x 122.020 x.860 meters Price: $18,28.03 House 3 Dimensions: 48.990 x 76.80 x 4.430 meters Price: $82,816.09 House 4

Dimensions: 44.860 x 4.260 x 7.300 meters Price: $12,422.44 House Dimensions: 191.0 x 172.760 x 6.020 meters Price: $31,98.9 2 Enter the number of the house to buy: John Smith is now a proud homeowner! 1 House 1 Dimensions: 184.490 x 10.220 x 6.10 meters Price: $47,110.37 House 2 Dimensions: 8.910 x 122.020 x.860 meters Price: $18,28.03 House 3 Dimensions: 48.990 x 76.80 x 4.430 meters Price: $82,816.09 House 4 Dimensions: 44.860 x 4.260 x 7.300 meters Price: $12,422.44

3 John Smith has sold their house to the market! 1 House 1 Dimensions: 184.490 x 10.220 x 6.10 meters Price: $47,110.37 House 2 Dimensions: 8.910 x 122.020 x.860 meters Price: $18,28.03 House 3 Dimensions: 48.990 x 76.80 x 4.430 meters Price: $82,816.09 House 4 Dimensions: 44.860 x 4.260 x 7.300 meters Price: $12,422.44 House Dimensions: 191.0 x 172.760 x 6.020 meters Price: $31,98.9

Goodbye! --------------------------------------------------------------------- Enter your name: Louise Oriondo Enter your age (in years): 21 Enter the amount of money you have: 123.69 You are: Name: Louise Oriondo Age: 21 years old Cash: $ 123.69 1 House 1 Color:RED Dimensions: 99.660 x 193.180 x.020 meters Price: $7,379.03 House 2 Dimensions: 39.770 x 38.270 x 9.010 meters Price: $30,116.9 House 3 Color:BLUE Dimensions: 3.880 x 18.460 x 7.660 meters Price: $66,437.30 House 4 Color:BLUE Dimensions: 19.80 x 188.320 x.230 meters Price: $29,321.83

House Color:GREEN Dimensions: 80.600 x 199.310 x 3.880 meters Price: $89,8.7 2 Enter the number of the house to buy: Louise Oriondo cannot afford this home. 3 Louise Oriondo has no home to sell. Goodbye! --------------------------------------------------------------------- Enter your name: Jaicob Stewart Enter your age (in years): 20 Enter the amount of money you have: 20000 You are: Name: Jaicob Stewart Age: 20 years old Cash: $ 20,000.00

2 Enter the number of the house to buy: Jaicob Stewart is now a proud homeowner! 2 Enter the number of the house to buy: 12 Invalid house number 2 Enter the number of the house to buy: 1 Jaicob Stewart is already a homeowner! Goodbye! --------------------------------------------------------------------- Enter your name: Becca Burns Enter your age (in years):

27 Enter the amount of money you have: 4000.12 You are: Name: Becca Burns Age: 27 years old Cash: $ 40,00.12 4 Name: Becca Burns Age: 27 years old Cash: $ 40,00.12 3 Becca Burns has no home to sell. Goodbye!