CSIS 10A Assignment 9 Solutions

Similar documents
Assignment 8B SOLUTIONS

CSIS 10A Assignment 3 Due: 2/21 at midnight

Lab Exercise 1. Objectives: Part 1. Introduction

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

CSIS 10A Assignment 4 SOLUTIONS

Chapter 5 Lab Methods

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

ing execution. That way, new results can be computed each time the Class The Scanner

Over and Over Again GEEN163

Example Program. public class ComputeArea {

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

H212 Introduction to Software Systems Honors

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

Fundamentals of Programming Data Types & Methods

Question 1 [20 points]

Code Listing H-1. (Car.java) H-2 Appendix H Packages

Chapter 5 Lab Methods

Software and Programming 1

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

CS 101 Spring 2007 Midterm 2 Name: ID:

1 Short Answer (10 Points Each)

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

1 Short Answer (5 Points Each)

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

Part 1 Dr King Book Exercises 1 12 pp

Chapter 6 Lab Classes and Objects

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Programming with Java

Chapter 6 Lab Classes and Objects

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

1 Short Answer (15 Points Each)

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)

CPS109 Lab 7. Source: Big Java, Chapter 7 Preparation: read Chapter 7 and the lecture notes for this week.

Chapter 5 Lab Methods

St. Edmund Preparatory High School Brooklyn, NY

Oct Decision Structures cont d

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

CS Week 13. Jim Williams, PhD

Netbeans tutorial:

Repetition, Looping. While Loop

Top-Down Program Development

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

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

Introduction to Java (All the Basic Stuff)

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

COMP 202 Java in one week

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Object Orientated Programming Details COMP360

CSCI 1103: File I/O, Scanner, PrintWriter

COMP 202. Java in one week

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

Methods Common to all Classes

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Lesson 7 Part 2 Flags

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Maximization and Minimization Problems. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CEN 414 Java Programming

Software and Programming 1

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Exercise 0.1 (Review from lectures no submission required)

Programming in the Large II: Objects and Classes (Part 1)

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming

Loops. GEEN163 Introduction to Computer Programming

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Software and Programming 1

CSCI 261 Computer Science II

CS2 Assignment A1S The Simple Shapes Package

Elementary Programming

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Object Oriented Programming in C#

Final Exam Practice. Partial credit will be awarded.

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Object Oriented Programming

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

Exercise 1: Class Employee: public class Employee { private String firstname; private String lastname; private double monthlysalary;

CS 101 Fall 2006 Midterm 3 Name: ID:

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

CAT.woa/wa/assignments/eclipse

Recitation 3 Class and Objects

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Exceptions Handeling

What two elements are usually present for calculating a total of a series of numbers?

Honors Computer Programming 1-2. Chapter 5 Exam Prep

Maintenance and Fuel log Overview Learn how the maintenance and fuel log works

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Questions Answer Key Questions Answer Key Questions Answer Key

Transcription:

CSIS 10A Assignment 9 Solutions Read: Chapter 9 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be answered in the Exercises file Algorithm Workbench 1. Given the Circle class included in the Exercises file,

a) public String tostring() return "Circle: radius = " + radius + ", area = " + getarea() ); b) public boolean equals(circle other) return (this.radius == other.radius); // parenthesis optional, 'this' is also optional // note that we can refer to other.radius // directly since we share the same class (Circle) c) public boolean greaterthan(circle other) return this.getradius > other.getradius; Part 3: Programming Challenges 3 points each 1) Create classes P1LandTract and P1LandTractDemo and solve the following problem: In reading the problem statement you can determine that a UML of the LandTract class would look like this: LandTract -length: double -width: double + LandTract() + LandTract(l: double, w: double) + getarea(): double + equals(landtract other): boolean + tostring(): String Here is the solution, and a Demo program: LandTract.java public class LandTract // instance variables - replace the example below with your own private double length; private double width; * Constructor for objects of class LandTract public LandTract()

length = 0; width = 0; public LandTract(double l,double w) length = l; width = w; public double getarea() return length*width; public String tostring() String str; str = ("length: "+length+"\nwidth: "+width+"\narea: "+this.getarea()); return str; public boolean equals(landtract L) boolean status; if(this.getarea()==l.getarea()) status = true; status = false; return status; P1LandTractDemo.java * Write a description of class P1LandTractDemo here. * * @author (your name) * @version (a version number or a date) import java.util.scanner; public class P1LandTractDemo static LandTract here; static LandTract there; public static void main(string[] args) defineland(); displayarea(); determineequality(); public static void defineland() double length; double width; Scanner keyboard = new Scanner(System.in);

System.out.println("enter the length for here:"); length = keyboard.nextdouble(); System.out.println("enter the width for here:"); width = keyboard.nextdouble(); here = new LandTract(length,width); System.out.println("enter the length for there:"); length = keyboard.nextdouble(); System.out.println("enter the width for there:"); width = keyboard.nextdouble(); there = new LandTract(length,width); public static void displayarea() double area; area = here.getarea(); System.out.println("The area here is: "+area); area = there.getarea(); System.out.println("The area there is: "+area); public static void determineequality() if(here.equals(there)) System.out.println("here and there are equal areas"); System.out.println("here and there are different areas"); 2) Create new classes called P2FuelGauge, P2Odometer, and P2CarSimulator that solves the following problem

This problem is not well defined, so we have some flexibility in how we set it up. The FuelGauge class needs to know how many gallons are currently in the tank. Therefore we need a member field called "gallons" to keep track of this information. We also need a way to add a gallon, and remove a gallon. Similarly, the odometer needs to know the mileage, report on the mileage, and increment the mileage by one gallon. From the problem statement the Odometer needs to have access to the FuelGauge object. There are at least two ways to do this: 1) We can give it access through the use of "Aggregation" like we learned in the lab -- that means including the car's FuelGauge object in the Odometer class's list of member fields and including a FuelGauge in the Odometer constructor; OR 2) we can pass our FuelGauge object to the incrementmileage method and let it change the FuelGauge whenever we've driven 24 miles. We'll go with number 1) for this solution. Why? Well, it seems like the Odometer needs to be connected to a particular FuelGauge for its entire existence. If we have to pass a FuelGauge to the Odometer every time we drive a mile, we might pass the "wrong" fuel gauge at some point (if we are simulating two cars for example). If we give the FuelGauge to the Odometer constructor, the odometer will always be able to change the FuelGauge object it started out with by referring to its own member field for it We'll also need some way to keep track of when we've driven enough to invoke the "decrementgallons" method so for this we'll create another field in Odometer to keep track of this mileage called setpoint. Here are two UML diagrams for the two classes described: With our classes defined as above, we can write the main app that runs the simulation: Chapter 9, Progrmming Challenge 10 Car Instrument Simulator public class CarInstrumentSimulator public static void main(string[] args)

// Create a FuelGuage object. FuelGauge fuel = new FuelGauge(); // Create an Odometer object to work with the FuelGauge object. Odometer odometer = new Odometer(0, fuel); // Fill the car up with gas. for (int i = 0; i < fuel.max_gallons; i++) fuel.incrementgallons(); // Drive the car until it runs out of gas. while (fuel.getgallons() > 0) // Drive a mile. odometer.incrementmileage(); // Display the mileage. System.out.println("Mileage: " + odometer.getmileage()); // Display the amount of fuel. System.out.println("Fuel level: " + fuel.getgallons() + " gallons"); System.out.println("------------------------------"); FuelGauge class public class FuelGauge // Constant for the maximum number of gallons final int MAX_GALLONS = 15; // Field for the amount of fuel, in gallons private int gallons; No-arg constructor public FuelGauge() gallons = 0; Constructor @param g The initial number of gallons. public FuelGauge(int g) // Set gallons to g, but no more than // the maximum amount. if (g <= MAX_GALLONS) gallons = g; gallons = MAX_GALLONS;

getgallons method @return The number of gallons of fuel. public int getgallons() return gallons; The incrementgallons method increments the value of gallons. If gallons exceeds the maximum amount, a message is displayed incicating the fuel is overflowing. public void incrementgallons() if (gallons < MAX_GALLONS) gallons++; System.out.println("FUEL OVERFLOWING!!!"); The decrementgallons method decrements the value of gallons. If gallons is at 0 then a message is displayed indicating we are out of fuel. public void decrementgallons() if (gallons > 0) gallons--; System.out.println("OUT OF FUEL!!!"); Odometer class public class Odometer // Constant for the maximum mileage public final int MAX_MILEAGE = 999999; // Constant for the miles-per-gallon public final int MPG = 24; // Field for the current mileage private int mileage; // Field for the mileage set-point to // remember when the FuelGuage gallons // were decremented. private int setpoint; // Field to reference a FuelGauge object private FuelGauge fuelgauge; Constructor @param m Initial mileage.

@param fg A reference to a FuelGauge object. public Odometer(int m, FuelGauge fg) mileage = m; setpoint = m; fuelgauge = fg; getmileage method @returns The mileage. public int getmileage() return mileage; The incrementmileage method increments the mileage field. If mileage exceeds the maximum amount, it rolls over to 0. public void incrementmileage() // Increment the mileage, but rollover // if we go past the maximum amount. if (mileage < MAX_MILEAGE) mileage++; // we are rolling over the odometer back to 0 mileage = 0; setpoint = setpoint MAX_MILEAGE; // change setpoint to refuel right // See if we have burned a gallon of gas. This // happens every MPG miles. // if ( (mileage - setpoint) >= MPG ) fuelgauge.decrementgallons(); setpoint = mileage;