AP CS Unit 4: Classes and Objects Programs

Similar documents
APCS Semester #1 Final Exam Practice Problems

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

AP CS Unit 6: Inheritance Notes

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

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

AP Computer Science A Unit 7. Notes on Arrays

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Chapter 4 Defining Classes I

Java Review. Fundamentals of Computer Science

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

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.

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

CS 302 Week 9. Jim Williams

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

AP CS Unit 7: Arrays Exercises

Advanced Java Concepts Unit 2: Linked Lists.

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

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

1 Short Answer (10 Points Each)

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Instance Method Development Demo

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Java and OOP. Part 2 Classes and objects

WES-CS GROUP MEETING #9

CSE 143 Lecture 10. Recursion

Chapter 6 Lab Classes and Objects

Chapter 6 Lab Classes and Objects

AP CS Unit 3: Control Structures Notes

CMP 326 Midterm Fall 2015

Classes Classes 2 / 36

Introduction to Java Programs for Packet #4: Classes and Objects

AP Computer Science Unit 1. Programs

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Final Exam Practice. Partial credit will be awarded.

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

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

Slide 1 CS 170 Java Programming 1

PROGRAMMING FUNDAMENTALS

Unit 4: Classes and Objects Notes

Unit 5: More on Classes/Objects Notes

Set<Integer> s = new TreeSet<Integer>(); s.add( 7 ); s.add( 7 ); System.out.println( s.size() );

CS Week 13. Jim Williams, PhD

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

Problem Grade Total

Unit 5: More on Classes/Objects Exercises

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

AP CS Unit 6: Inheritance Exercises

AP CS Unit 8: Inheritance Exercises

Programming Problems 22nd Annual Computer Science Programming Contest

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CS 1302 Chapter 9 (Review) Object & Classes

CS Week 14. Jim Williams, PhD

Object Oriented Programming

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

CSE 11 Midterm Fall 2008

Key Java Simple Data Types

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Lecture 3. Lecture

AP CS Unit 7: Interfaces. Programs

Introduction to the Java Basics: Control Flow Statements

Practice Midterm 1. Problem Points Score TOTAL 50

CS 101 Fall 2006 Midterm 3 Name: ID:

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

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

Documenting, Using, and Testing Utility Classes

ASSIGNMENT 4 Classes and Objects

Rules and syntax for inheritance. The boring stuff

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

AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

Lecture 15. For-Loops

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

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Chapter 7 Classes & Objects, Part B

C# Programming for Developers Course Labs Contents

Java Classes - Using your classes. How the classes you write are being used

Java Foundations: Unit 3. Parts of a Java Program

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

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

CHAPTER 7 OBJECTS AND CLASSES

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

Unit 4: Classes and Objects Exercises

Abstract classes are used to define a class that will be used only to build new classes. No objects will ever be instantiated from an abstract class.

AP Computer Science A Summer Assignment 2017

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CS 1331 Exam 1 ANSWER KEY

Express Yourself. Writing Your Own Classes

To keep track of this new wrinkle we need some new variables at the Class level:

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

Practice problem on defining and using Class types. Part 4.

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

CMSC 341 Lecture 7 Lists

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

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)

Transcription:

AP CS Unit 4: Classes and Objects Programs 1. Copy the Bucket class. Make sure it compiles (but you won t be able to run it because it does not have a main method). public class Bucket { private double amt; private final double CAPACITY; Add a second class (named BucketRunner) that does contain a main method. The main method should do the following: create a Bucket object that can hold 10 gallons. add a random amount using Math.random. The amount should be a decimal between 5 (inc) and 12 (exc). display how full the bucket is (e.g. 74%) empty the bucket call the percentfull method again and display the returned value ( it should be zero). 2. Copy the Walker method and complete the walk method. Then make sure it compiles. Add a second class (named Runner) and enter this code: public class Runner { public static void main(string[] args) { Walker w1 = new Walker(); w1.walk( 5 ); w1.walk( 10 ); w1.walk( 8 ); System.out.println( "w1 is now at " + w1.getx() ); Run it and make sure it works. 3. Add a third class that contains a main method. In this main method create another walker and keep calling the walk method (with an argument of 10) until its x value is 100 or greater. After it reaches 100, display its x value and the number of times the walk method was called. public Bucket( double c ) { CAPACITY = c; // gallons amt = 0; public void emptybucket() { amt = 0; public void add ( double stuff ) { amt += stuff; if ( amt > CAPACITY ) amt = CAPACITY; public double percentfull( ){ return 100 * amt / CAPACITY; public class Walker { private int x; public Walker() { x = 0; public void walk( int max ){ /* If max is less than one this method does nothing. If max is positive, then this method generates a random number between zero and max (inclusive) and increases x by that amount. */ public int getx(){ return x;

4. Copy and complete the CoinTosser class. An object of this class is someone who really likes tossing a coin and seeing how many times they get heads. Create another class with a main method. In that method: Instantiate two CoinTosser objects using these two names. Have each CossTosser toss his/her coin 100 times. (In other words, call the toss method). Determine who had the most heads and print out something like: #1 wins, 54.0% to 47.0% If it s a tie, say so. 5. Create another class with a main method. This is similar to the above exercise. You will again enter 2 names and instantiate 2 CoinTosser objects. But this time they will play 1,000 contests (where each contest consists of 100 tosses). This means that the main method should contain a for-loop. At the end of each iteration you need to call the reset method. Print out the results of each contest and at the end print out the number of wins for each. The results should look something like this: #1 wins, 60.0% to 47.0% #1 wins, 50.0% to 41.0% It is a tie! Both had 47.0%... #1 wins, 52.0% to 45.0% #2 wins, 56.00000000000001% to 46.0% #1 wins, 51.0% to 48.0% public class CoinTosser { private int numtosses; private int numheads; public CoinTosser() { numtosses = 0; numheads = 0; public void toss(int n){ numtosses += n; for ( int k = 1; k <= n; k++ ){ if ( heads() ) numheads++; public void reset() { numtosses = 0; numheads = 0; private boolean heads() { // half the time it returns true public double getpercentheads() { /* returns percentage of heads. For example, if 3 heads out of 4 tosses, returns 0.75 */ #1 had 546 wins #2 had 452 wins #1 wins!

6. First, complete the Star class. public class Star{ private int x, y, z; public Star(){ // assign the instance variables random values between 0 and 20 // write accessor methods for the instance variables public double distance( int x1, int y1, int z1 ){ // returns the distance from (x, y, z) to (x1, y1, z1) public void display(){ System.out.println( "coordinates: " + x + ", " + y + ", " + z ); Second, write a runner class that creates three Star objects and calls their display methods. Then it calculates (using the distance method) and displays the distance between the three stars. If the stars create an isosceles or equilateral triangle, it displays a message to that effect. Otherwise it determines and displays which two stars are closest to each other. Here s a sample output: Star 1 coordinates: 15, 20, 18 Star 2 coordinates: 7, 14, 15 Star 3 coordinates: 14, 18, 20 The distance from star 1 to star 2 is 10.44030650891055 The distance from star 2 to star 3 is 9.486832980505138 The distance from star 3 to star 1 is 3.0 Star 3 and Star 1 are the closest 7. Complete any three exercises from the String1 category at codingbat.com. 8. Complete any five exercises from the String2 category at codingbat.com. 9. Complete any three exercises from the String3 category at codingbat.com.

10. First, complete the code below to create a Plant class. public class Plant { private String name; private boolean living; //create a constructor (no parameters), alive = true, name =??? // write a mutator method for name // write an accessor method for name // write an addwater method that has an int parameter that represents the water added. If the amount of water added is less than 20 or greater than 80, then set living equal to false. Otherwise do nothing. Note: if a Plant is dead, then adding any amount of water to it cannot bring it back to life. public String tostring() { // returns the name and whether the plant is alive or not. Second, write a class that has a main method. The main method should: Instantiate three Plant objects. Adds a random amount of water (between 0 and 100 units) to each plant. After watering the plants, call the tostring method for each plant. 11. First, complete the code below to create a Sentence class. Note, consider any series nonwhite characters as a word. For example, the following count as words: mom, 34T, and?$_q. public class Sentence{ private String stringy; // constructor with a String parameter // write an accessor method for stringy public String trim(){ /* return a string that is identical to stringy except that there is exactly one space between each word in stringy. For example, if stringy is cat dog then this method returns cat dog (with one space between cat and dog). */ public int count(){ // returns the number of words in stringy. May return zero. // this is a lot easier if you call the above trim method within this method Create a main method that asks the user to enter three sentences. Create three Sentence objects and call the trim and count methods. Display the return values.

12. Complete the Name class public class Name { private String first, middle, last; public Name( String str ) { check_name( str ); private void check_name( String nm ){ nm contains a name such as John Jacob Jingleheimer Schmidt or Tuhin Gupta. You may assume that there is at least a first and last name in nm. You need to break nm into first, middle, and last names and correctly assign them to the instance variables. You may assume that nm has at least two parts. If there are 2 parts then middle should be a blank String. If there are 3 parts, then you know what to do. If there are more than 3 parts, then assign part 1 to first, part 2 to middle, and everything else to last. The first thing this method should do is call the remove_spaces method. private String remove_spaces( String s ){ This returns a String that is identical to s except that all leading and trailing spaces have been removed. In addition, if there are two or more adjacent spaces then all but one are removed. For example: if s is " that's my name too " then it returns "that's my name too" public String full_name(){ Returns first + middle + last but there should be exactly one space between each part. If there is no middle name then there should be only one space between first and last public String tostring(){ return last + ", " + first + " " + middle; Write a second class that has a main method. Have the user enter their name. Instantiate a Name object and then call the full_name and tostring methods and display the results.