Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class



Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

CS 111 Classes I 1. Software Organization View to this point:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Introduction to Java

Java Interview Questions and Answers

Part I. Multiple Choice Questions (2 points each):

Sources: On the Web: Slides will be available on:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Python

Statements and Control Flow

Moving from CS 61A Scheme to CS 61B Java

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

AP Computer Science Java Subset

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Sample CSE8A midterm Multiple Choice (circle one)

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Unit 6. Loop statements

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Java CPD (I) Frans Coenen Department of Computer Science

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Introduction to Python

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Example of a Java program

Object Oriented Software Design

Lecture 5: Java Fundamentals III

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Contents. 9-1 Copyright (c) N. Afshartous

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Object Oriented Software Design

The C Programming Language course syllabus associate level

13 Classes & Objects with Constructors/Destructors

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Programming Languages CIS 443

Iteration CHAPTER 6. Topic Summary

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Crash Course in Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 106 Introduction to Computer Science I

An Incomplete C++ Primer. University of Wyoming MA 5310

CS170 Lab 11 Abstract Data Types & Objects

Software Engineering Techniques

13 File Output and Input

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Basic Programming and PC Skills: Basic Programming and PC Skills:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computer Programming I

Programming Database lectures for mathema

Java (12 Weeks) Introduction to Java Programming Language

CS193j, Stanford Handout #10 OOP 3

CS 241 Data Organization Coding Standards

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

java Features Version April 19, 2013 by Thorsten Kracht

El Dorado Union High School District Educational Services

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

PA2: Word Cloud (100 Points)

Java Crash Course Part I

PostgreSQL Functions By Example

Assignment 3 Version 2.0 Reactive NoSQL Due April 13

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

CMSC 202H. ArrayList, Multidimensional Arrays

Chapter 2 Introduction to Java programming

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Part 3: GridWorld Classes and Interfaces

Java Programming Language

CSE 1020 Introduction to Computer Science I A sample nal exam

1. Writing Simple Classes:

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Affdex SDK for Android. Developer Guide For SDK version 1.0

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

NLP Programming Tutorial 0 - Programming Basics

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Illustration 1: Diagram of program function and data flow

Chapter 13 Storage classes

Java Application Developer Certificate Program Competencies

Pemrograman Dasar. Basic Elements Of Java

HW3: Programming with stacks

In this Chapter you ll learn:

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Introduction to Computer Programming (CS 1323) Project 9

Informatica e Sistemi in Tempo Reale

Pseudo code Tutorial and Exercises Teacher s Version

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Programming System Design. CSCI 455x (4 Units)

Transcription:

CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class This homework is to be done individually. You may, of course, ask your fellow classmates for help if you have trouble editing files, compiling programs, or using the submit program. Do not share code or specific parts of answers. Please see the syllabus for guidelines on appropriate interaction with your fellow classmates. Homeworks will often require written answers as well as small programs (Java source files). All written answers are to be placed in a file called writeup.txt. This file should be a plain text file. Make sure this file is readable by wordpad or emacs or similar text-only editor. No credit will be given for written answers stored in a non-text format. Please ask your TA for help if you have trouble. When you are finished with the homework, you will submit it electronically. First put all of the files you want to submit into their own directory (including writeup.txt). Change to that directory, and then type submit hw2 For this homework you can put all your work into writeup.txt, or you can include additional files as well. If you include additional files, make sure to reference those fiels in the appropriate place in your writeup.txt file so we can find them and will know which files have which answers. Thanks. If you can t complete the entire homework, and it complains about you not having certain files, then just edit a blank file with that name, and then save it. This will submit an empty file, but the submit program should then work. Problem 1 1.1) The process of turning Java source files (ending in.java) into machine-readable files that can be executed is called 1.2) Given these lines of code: int i = 3; float f = 3.14f; long L = 23L; double d = 22.3; Which of these statements will compile?

i = L; L = i; f = i; d = f; f = d; f = (float) d; f = (int) d; d = (double) ((float) d); Problem 2 2.1 What is the result of the following lines of code? for (int i = 0; i < 10; i = i+2) { if (i % 3 == 1) System.out.println(i * 2); 2.2 Write the following switch statement as nested and daisy-chained if statements: String val; int i = getsomeinteger(), k = getsomeotherinteger(); switch (i) { case 0: val = str1 ; break; case 1: val = null; break; case 2: if (k % 3 == 0) { val = str2 ; break; case 3: case 4: case 5: val = str3 ; default: val = str4 ;

2.3 What is the difference between for (init; test; update) { statements; and init while (test) { statements; update; (Hint: what could statements contain that would affect the flow in a loop? We talked in class about two keywords which, when put into loops, affect the way they execute). Problem 3 3.1 Fill in the following method body. This method should return an array of size N, where the i th entry consists of the sum of the first i integers (starting at 0). So for example, if N == 4 the array returned would be: { 0, 1, 3, 6 public int[] sumupto(int N) { 3.2 Declare an array of String object references called stars Make the i th element of that array refer to a String of length i that contains only the * character. For example, the 3 rd element of the stars array would point to a String object representing **

Problem 4 In this problem, we are going to design and build a StampMachine class. A stamp machine is a vending machine which takes money as input, and gives the user stamps as output. The StampMachine class has the following methods: A default constructor an addstamp method which takes a Stamp object as input and has a void return type a buystamp method which takes an int argument representing how many cents the stamp is worth. This method returns a Stamp object of that value (if such a stamp is in the machine). If there is no stamp of that value in the machine, then the method return null. Once a stamp is purchased is should be removed from the machine a getmoney method which takes no arguments. It returns the amount of money stored in the machine (from when people bought stamps) in the form of an int. A side-effect of this method is that the current count of how much money is in the machine is reset to 0. Example: StampMachine sm = new StampMachine(); Stamp s1 = new Stamp(37); Stamp s2 = new Stamp(37); Stamp p1 = new Stamp(385); sm.add(s1); sm.add(s2): sm.add(p1); Stamp mystamp = sm.buystamp(385); Stamp mystamp2 = sm.buystamp(37); Stamp mystamp3 = sm.buystamp(33); // returns null int totalmoney = sm.getmoney(); // totalmoney is now 422 sm.getmoney(); // returns 0 now Here is the Stamp class (NOTE: It is very much like the Book class we discussed in lecture. Note the equals() method, meaning that the Java API library will usually compare two Stamp objects based on their value, in this case. public class Stamp { private int value;

public Stamp(int v) { value = v; public String tostring() { return new String(value); public boolean equals(object o) { if (!(o instanceof Stamp)) { return false; else if (this == (Book) o) { return true; Stamp s = (Stamp) o; return this.value == o.value; 4.1 How do you want to internally keep track of 1) the stamps in the system, and 2) the amount of money in the machine? In class we talked about using Java arrays (so Stamp[]) and we talked about ArrayList. Choose either of these options, or if you prefer to do it a different way, that is ok too. 4.2 Write out the class definition (leave the method bodies empty for now) 4.3 Now write definitions for each of the methods in your class. Problem 5 Consider the following classes: public class Animal { public void sleep(); public void wakeup();

public class Feline extends Animal { public void roam(); public class Cat extends Animal { public void makenoise(); public class Lion extends Animal { public void makenoise(); 5.1 Which of the following lines will compile? Animal a = new Animal(); Feline f = new Feline(); Animal x = new Feline(); Feline y = new Cat(); y.makenoise(); 5.2 When I call: Cat c = new Cat(); 5.3 Consider: public class A { public void foo(); public class B extends A { public void foo(); 5.3 What is it called when class B provides a different foo () method than its superclass? 5.4 When I call B b = new B(); b.foo();

which foo() gets called? Is the same true for when I access field variables?