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

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

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

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

Chapter 4 Defining Classes I

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 302: Introduction to Programming in Java. Lecture 15

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

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

AP CS Unit 4: Classes and Objects Programs

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

TaxiBot New attributes Variables Math! TaxiBot

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

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

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Final Examination Semester 3 / Year 2012

Today s Agenda. Quick Review

CONSTRUCTORS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 19. By Matthew Bernstein

Final Examination Semester 3 / Year 2012

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

52 Franck van Breugel and Hamzeh Roumani

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

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Chapter 4: Writing Classes

CSC 1051 Algorithms and Data Structures I. Final Examination December 17, Name:

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

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

Software and Programming 1

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Write the code to create an enhanced for loop that will go through every member of an ArrayList <String> myarray and print it out to the console.

UNDERSTANDING CLASS DEFINITIONS CITS1001

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

Chapter 12: How to Create and Use Classes

Instance Method Development Demo

Objects as a programming concept

Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

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.

A+ Computer Science -

CS 2530 INTERMEDIATE COMPUTING

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Understanding class definitions

CS 302 Week 9. Jim Williams

Computer Programming

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

AP COMPUTER SCIENCE AB 2006 SCORING GUIDELINES

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

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

enum: Enumerated Type An "enum" is a type with a fixed set of elements.

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

Lesson 14: Abstract Classes and Interfaces March 6, Object-Oriented S/W Development with Java CSCI 3381

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

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

If too much money is inserted the machine takes it all - no refund. If there isn't enough money inserted, it still prints out the ticket.

CS 113 MIDTERM EXAM 2 SPRING 2013

CMSC 202H. Classes and Objects: Reusing Classes with Composition

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

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

3. Can an abstract class include both abstract methods and non-abstract methods? D

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

CS1004: Intro to CS in Java, Spring 2005

CSE 413 Winter 2001 Midterm Exam

Week 9. Muhao Chen

Ticket Machine Project(s)

by Pearson Education, Inc. All Rights Reserved. 2

Week 11: Class Design

CS 1331 Exam 1 ANSWER KEY

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

CSCI 1226 Second Midterm Test

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

CS1083 Week 2: Arrays, ArrayList

Robots. Byron Weber Becker. chapter 6

Midterm Exam 5 April 20, 2015

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

Encapsulation in C++

Programming Exercise 14: Inheritance and Polymorphism

CPSC 219 Extra review and solutions

Informatik II Tutorial 6. Subho Shankar Basu

The Singleton Pattern. Design Patterns In Java Bob Tarr

9/17/2018 Programming Data Structures. Encapsulation and Inheritance

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.

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

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

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

1 (5 marks) 2 (6 marks) 3 (6 marks)

Package. A package is a set of related classes Syntax to put a class into a package: Two rules: Example:

CSC207 Week 2. Larry Zhang

Chapter 6 Introduction to Defining Classes

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

CS100J Prelim I, 29 Sept. 2003

Chapter 9 Enumerations

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

More about inheritance

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

Transcription:

Quiz 6 Name: 1. Suppose you have a class Cat defined as shown below. Fill in the code for the indicated methods, following guidelines given through comments. public class Cat // instance variables String name; int age; int lives; // number of lives remaining // 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 // birthday(): if the cat is still alive (has at least one // life left), increases age by 1; otherwise does nothing // WRITE METHOD DEFINITION HERE: ******************************* // ************************************************************* // death(): decreases the number of lives by 1; no effect // on dead cats (i.e., cats with 0 lives). // ********** assume this is already implemented ********* // getage(): returns the cat s age // ********** assume this is already implemented ********* // getlives(): returns the cat s remaining lives // ********** assume this is already implemented ********* // tostring() // WRITE METHOD DEFINITION HERE: ******************************* // *************************************************************

2) Suppose you are writing a driver class that uses the Cat class. Answer the questions below by writing code fragments for this driver class. a) Write some code to instantiate an object of the Cat class with name Luca and assign it to a variable named myprofscat b) Suppose you have already declared and initialized three variables cat1, cat2, and cat3 that refer to Cat objects. Let s pretend that two years have gone by So their ages need to increase. Meanwhile, cat3 has done something stupid and loses a life. Write some code that uses the birthday() and death() methods to model this situation. c) Write some code to print the sum total of the lives of cat1, cat2, and cat3.

Quiz 6 Name: 1. Suppose you have a class Car defined as shown below. Fill in the code for the missing methods, following guidelines given through comments. public class Car // instance variables String manufacturer; String model; int year; double price; // constructor public Car(String x, String y, int z, double w) manufacturer = x; model = y; year = z; price = w; //constructor: another version of the constructor, without // parameters for year, or price; sets these to default // values (2013 and 0, respectively). // tostring() // getprice()

2) Suppose you are writing a driver class that uses the Car class. Answer the questions below by writing code fragments for this driver class. a) Write some code to instantiate an object of the Car class (using the second constructor) and to assign this object to a variable named obscureobjectofdesire. Use this information for the car: manufactured by Jaguar, model name XF Supercharged. b) Suppose you have already declared and initialized three variables car1, car2, and car3 that refer to Car objects. Write some code to find the average of the car prices and assign it into a variable named priceavg. e) Write some code to reduce the price of car2 by $5000. (So, for example, if the current price is $30000, it should be reduced to $25000. ) Use the accessor and mutator methods.

Quiz 6 Name: 1. Suppose you have a class Cat defined as shown below. Fill in the code for the missing methods, following guidelines given through comments. public class Cat // instance variables private String name; private int age; private int lives; // number of lives remaining // constructor public Cat(String x, int y) name = x; age = y; lives = 9; // start with 9 lives // age accessor // age mutator // tostring()

2) Suppose you are writing a driver class that uses the Cat class. Answer the questions below by writing code fragments for this driver class. a) Write some code to instantiate an object of the Cat class, named Luca, aged 5, and assign it to a variable named myprofscat b) Write some code to print the information about myprofscat c) Suppose you have already declared and initialized three variables cat1, cat2, and cat3 that refer to Cat objects. Write some code to calculate and print the average of the ages of cat1, cat2, and cat3.

Quiz 6 Name: 1. Suppose you have a class Car defined as shown below. Fill in the code for the missing methods, following guidelines given through comments. public class Car // instance variables String manufacturer; String model; int year; double price; // constructor public Car(String x, String y, int z, double w) manufacturer = x; model = y; year = z; price = w; // price mutator // price accessor // tostring()

2) Suppose you are writing a driver class that uses the Car class. Answer the questions below by writing code fragments for this driver class. a) Write some code to instantiate an object of the Car class and to assign this object to a variable named obscureobjectofdesire. Use this information for the car: manufactured by Jaguar, model name XF Supercharged, year 2013, and price $68,100. b) Write some code to print the information about obscureobjectofdesire c) Suppose you have already declared and initialized three variables car1, car2, and car3 that refer to Car objects. Write some code to calculate and print the average price of these cars. d) Write some code to set the price of car1 to $25,000.