CS151 Principles of Computer Science I Fall 2018 Homework 6 S

Similar documents
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Object-Oriented Programming Concepts

Recommended Group Brainstorm (NO computers during this time)

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Building Java Programs

UFCE3T-15-M Object-oriented Design and Programming

2 What are the differences between constructors and methods?

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

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

CS1004: Intro to CS in Java, Spring 2005

Comments are almost like C++

COS 226 Algorithms and Data Structures Week 3: Comparators, & Sorting (Video 5.D & Algorithms 2.1 and 2.2)

CS 113 PRACTICE FINAL

Objects as a programming concept

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

Lecture 11: Intro to Classes

Software and Programming 1

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.

Assignment 8B SOLUTIONS

Final Exam COMP Fall 2004 Dec 16, 2004

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

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

public class SomeClass OtherClass SomeInterface { }

Object Oriented Programming COP3330 / CGS5409

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Object Oriented Programming in C#

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

Chapter 9. Objects and Classes

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

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Simple Java Reference

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.

MACS 261J Final Exam. Question: Total Points: Score:

Building Java Programs

OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

Chapter 6 Lab Classes and Objects

CS 1331 Exam 1 ANSWER KEY

Building Java Programs

Making New instances of Classes

What will this print?

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 6 Lab Classes and Objects

Software 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)

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

Unit 10: Sorting/Searching/Recursion

Unit 10: Sorting/Searching/Recursion

CS1083 Week 2: Arrays, ArrayList

CSCI 102 Fall 2010 Exam #1

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

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

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

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

Java Comparable interface

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

CS 101 Fall 2006 Midterm 3 Name: ID:

Chapter 3 Objects and Classes

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

Object-Oriented Programming (Java)

Java and OOP. Part 2 Classes and objects

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Another IS-A Relationship

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Assignment 1 due Monday at 11:59pm

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

ECOM 2324 COMPUTER PROGRAMMING II

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Chapter 4 Defining Classes I

CS 302 Week 9. Jim Williams

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Computer Programming, I. Laboratory Manual. Final Exam Solution

Timing for Interfaces and Abstract Classes

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

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

CMP 326 Midterm Fall 2015

CS 170, Section /3/2009 CS170, Section 000, Fall

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

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

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

CMPS 12A - Winter 2002 Midterm 2 March 5, Name: ID:

CS 251 Intermediate Programming Inheritance

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

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

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CS Computers & Programming I Review_01 Dr. H. Assadipour

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

CSIS 10A Assignment 9 Solutions

Topics. Passing objects to functions. Sections 7.9, 7.10, Objects and functions

CS2 Assignment A1S The Simple Shapes Package

CS 1331 Fall 2016 Exam 2

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

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Notes on Chapter Three

Question: Total Points: Score:

Transcription:

1. Exercise 19 : double Exercise 20 : String CS151 Principles of Computer Science I Fall 2018 Homework 6 S Exercise 21 : foo1 is a class method since it is declared static Exercise 22 : foo2 is an instance method since it is not declared static Exercise 23 : double ans = Airplane.foo1("O Hare"); Exercise 24 : String s = a1.foo2( C ); Exercise 25 : public static void foo3(double d). It s possible that foo3 might have a return value that is ignored by the example code, but it s more likely that it s return type is void. Exercise 26 : public int foo4(string s) 2. The following is one possible implementation of the TelevisionChannel class. To save space I ve listed some methods next to each other public class Television private String name; private int number; private boolean cable; // constructors public Television() public Television(String n, int num, boolean c) name = ""; name = n; number = 0; setnumber(num); cable = false; cable = c; // accessors public String getname() public int getnumber() return name; return number; public boolean getcable() return cable; // mutators public void setname(string n) public void setnumber(string num) name = n; if (num <= 0) number = 0; public void setcable(boolean val) number = num; cable = val;

public String tostring() String ans = name + ":" + number; if (cable) ans += "(C)"; return ans; public boolean equals(televisionchannel other) if (!name.equals(other.name)) return false; if (number!= other.number) return false; return (cable == other.cable); public int numdigits() return (number+"").length(); public String channeltype() if (cable) return "cable"; return "network"; 3. Below is one possible solution. To save space all of my header comments have been removed save for one or two to show you what I expect. public class Circle private double xc, yc, radius; // constructors public Circle(double centerx, double centery, double r) xc = centerx; yc = centery; setradius(r); // accessors public double getxcenter() public double getycenter() return xc; return yc;

public double getradius() return radius; // mutators public void setxcenter(double newx) public void setycenter(double newy) xc = newx; yc = newy; public void setradius(double r) if (r < 0.0) radius = 0.0; radius = r; public boolean equals(circle c) / equals - determine if two circles are identical Circle c - the circle to compare this circle to return value: true if c has the same radius and center as this circle false otherwise / return (xc == c.xc && yc == c.yc && radius == c.radius); public String tostring() return "(" + xc + "," + yc + ") r=" + radius; public boolean inside(double x, double y) / inside - determine whether a given point lies inside this circle double x, y - coordinates of the point return value: true if point (x,y) lies within this circle false otherwise / double dx = xc -x; double dy = yc -y; return Math.sqrt(dxdx + dydy) <= radius;

public int compareto(circle c) / compareto - compares the sizes of two circles Circle c - the circle to compare this circle to return value: -1, if this circle is smaller than c 0, if this circle is same size as c 1, if this circle is larger than c / if (radius < c.radius) return -1; if (radius > c.radius) return 1; return 0; public boolean intersect(circle c) double dx = xc -c.xc; double dy = yc -c.yc; return Math.sqrt(dxdx + dydy) <= radius + c.radius; public static void main(string[] args) Circle c1 = new Circle(0,0,5); Circle c2 = new Circle(3, 2, 10); Circle c3 = new Circle(0,0,5); System.out.println("x coord = " + c1.getxcenter()); System.out.println("y coord = " + c1.getycenter()); System.out.println("radius = " + c1.getradius()); c1.setxcenter(3); c1.setycenter(2); c1.setradius(-10); c1.setradius(10); if (c1.equals(c2)) System.out.println("Circle " + c1 + " and circle " + c2 + " are the same"); System.out.println("Circle " + c1 + " and circle " + c2 + " are the different");

if (c1.equals(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " are the same"); System.out.println("Circle " + c1 + " and circle " + c3 + " are the different"); if (c1.inside(5.0, 5.0)) System.out.println("point (5,5) inside circle " + c1); System.out.println("point (5,5) not inside circle " + c1); if (c3.inside(5.0, 5.0)) System.out.println("point (5,5) inside circle " + c3); System.out.println("point (5,5) not inside circle " + c3); System.out.println("output of c1.compareto(c2) = " + c1.compareto(c2)); System.out.println("output of c1.compareto(c3) = " + c1.compareto(c3)); System.out.println("output of c3.compareto(c2) = " + c3.compareto(c2)); if (c1.intersect(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " intersect"); System.out.println("Circle " + c1 + " and circle " + c3 + " do not intersect"); c3 = new Circle(50, 50, 4); if (c1.intersect(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " intersect"); System.out.println("Circle " + c1 + " and circle " + c3 + " do not intersect");