Quiz 7 Worksheet for CMSC 131: Java Programming, Quizzes of Computer Science

Information about quiz 7 for the cmsc 131 java programming course. It includes instructions for the written quiz, which covers material from exercises on methods, stacks, and arrays. Students are expected to write code for problems involving implementing double.parsedouble, creating a mystack class, extending the student class to create a roster class, and generating an array of even random numbers.

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-g3i
koofers-user-g3i 🇺🇸

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 131 Quiz 7 Worksheet
The next quiz will be on Monday, Nov 8 during your lab session (either at 10 am or 11
am). The following list provides more information about the quiz:
.
It will be a written quiz (not using any computer).
It will be closed-book, closed-notes, and no calculator is allowed.
Answers must be neat and legible. We recommend that you use pencil and eraser.
The quiz will be based on the exercises you will find below.
The following exercises cover the material to be covered in Quiz #7. Solutions to these
exercises will not be provided, but you are welcome to discuss your solutions with TAs
and instructors during office hours.
When asked for a “piece of code” you do not need to provide an entire class definition or
even an entire method. Just present the Java statements and any variable declarations as
needed to make your solution clear.
Problem 1
Write a method that implements the functionality of Double.parseDouble. That is, a
method that transforms a string into a double. Examples include “14.234”, “-13”,
“+.234”, and “0.0100”.
Problem 2
A Stack is a data structure frequently used in Computer Science. In this structure
elements are inserted and removed from the same end (similar to a stack of clean plates in
a dinner). Implement a class called MyStack which represents a Stack of String objects.
The specifications associated with the class are:
Instance variable
String[] array; // Array used to represent the stack.
// Feel free to add any other variables you may need.
Methods
Constructor – Creates a stack with no elements. Hint: create an array with size 0.
push – Adds an element to the top of the stack. It takes as parameter a reference to
a string. The method will create a duplicate of the string and will add the copy to
the stack. The array must be resized to accommodate the new element.
pf3

Partial preview of the text

Download Quiz 7 Worksheet for CMSC 131: Java Programming and more Quizzes Computer Science in PDF only on Docsity!

CMSC 131 Quiz 7 Worksheet

The next quiz will be on Monday , Nov 8 during your lab session (either at 10 am or 11 am). The following list provides more information about the quiz: .

  • It will be a written quiz (not using any computer).
  • It will be closed-book, closed-notes, and no calculator is allowed.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.
  • The quiz will be based on the exercises you will find below.

The following exercises cover the material to be covered in Quiz #7. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.

When asked for a “piece of code” you do not need to provide an entire class definition or even an entire method. Just present the Java statements and any variable declarations as needed to make your solution clear.

Problem 1

Write a method that implements the functionality of Double.parseDouble. That is, a method that transforms a string into a double. Examples include “14.234”, “-13”, “+.234”, and “0.0100”.

Problem 2

A Stack is a data structure frequently used in Computer Science. In this structure elements are inserted and removed from the same end (similar to a stack of clean plates in a dinner). Implement a class called MyStack which represents a Stack of String objects. The specifications associated with the class are:

Instance variable

String[] array; // Array used to represent the stack. // Feel free to add any other variables you may need.

Methods

  • Constructor – Creates a stack with no elements. Hint: create an array with size 0.
  • push – Adds an element to the top of the stack. It takes as parameter a reference to a string. The method will create a duplicate of the string and will add the copy to the stack. The array must be resized to accommodate the new element.
  • pop – Removes an element from the top of the stack. It takes no parameters. The method removes the string at the top of the stack and resizes the array. The method returns a reference to the string that was removed.
  • top – Returns a reference to the element at the top of the stack without removing the element.
  • empty – Returns true if the stack is empty and false otherwise.

The following illustrates how to use the class MyStack:

MyStack myStack = new MyStack(); myStack.push(“Hello”); myStack.push(“Everybody”); System.out.println(myStack.top()); // It will print “Everybody”. myStack.pop(); System.out.println(myStack.top()); // It will print “Hello”.

Problem 3

A student class has the following definition:

public class Student { private String name; private int id;

public Student(String sname, int sid) { name = sname; id = sid; }

public Student(Student student) { name = new String(student.name); id = student.id; }

public int getId() { return id; }

public String toString() { return name + " " + id; } }

Define a class called Roster which has the following specifications:

Instance variable

Student[] array; // array used to represent the roster