UNIVERSITY OF MANCHESTER

Similar documents
D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

D06 PROGRAMMING with JAVA

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

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

Moving from CS 61A Scheme to CS 61B Java

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

Java: overview by example

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Introduction to Java

AP Computer Science Java Subset

Inheritance, overloading and overriding

Lecture J - Exceptions

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

CS 106 Introduction to Computer Science I

CS170 Lab 11 Abstract Data Types & Objects

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Programming Languages CIS 443

Chapter 13 - Inheritance

Java Classes. GEEN163 Introduction to Computer Programming

Variables, Constants, and Data Types

AP Computer Science Java Mr. Clausen Program 9A, 9B

WRITING DATA TO A BINARY FILE

Java CPD (I) Frans Coenen Department of Computer Science

Sample CSE8A midterm Multiple Choice (circle one)

D06 PROGRAMMING with JAVA

AP Computer Science Static Methods, Strings, User Input

Building Java Programs

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

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

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Java Interview Questions and Answers

D06 PROGRAMMING with JAVA

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Java Basics: Data Types, Variables, and Loops

Using Files as Input/Output in Java 5.0 Applications

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

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

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:

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

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

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

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Building Java Programs

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

Java Crash Course Part I

CMSC 202H. ArrayList, Multidimensional Arrays

CISC 181 Project 3 Designing Classes for Bank Accounts

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

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

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Chapter 1 Java Program Design and Development

Chapter 2: Elements of Java

Lab Experience 17. Programming Language Translation

Iteration CHAPTER 6. Topic Summary

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

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

ICOM 4015: Advanced Programming

Crash Course in Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

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

Java Program Coding Standards Programming for Information Technology

Project 2: Bejeweled

Introduction to Java. CS 3: Computer Programming in Java

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

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

Conditionals (with solutions)

Homework/Program #5 Solutions

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

C++ INTERVIEW QUESTIONS

Introduction to Object-Oriented Programming

Chapter 2 Introduction to Java programming

Programming to Interfaces

C Compiler Targeting the Java Virtual Machine

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

D06 PROGRAMMING with JAVA

Semantic Analysis: Types and Type Checking

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

6.1. Example: A Tip Calculator 6-1

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

CSE 308. Coding Conventions. Reference

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

More on Objects and Classes

This explanations are for absolute beginners. Skilled programmers should (and probably will) use more effective approach.

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

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

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Fondamenti di C++ - Cay Horstmann 1

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Fundamentals of Programming and Software Development Lesson Objectives

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

13 File Output and Input

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

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

Stacks. Linear data structures

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Transcription:

COMP67021 Answer Sheet to be provided Two Hours UNIVERSITY OF MANCHESTER High Level Programming (Java) January 2009 14:00 16:00 Answer ALL Questions in Sections A and B and one question from Section C Electronic calculators may be used, provided that they cannot store text. University of Manchester, 2009 PTO

SECTION A (Answer ALL questions) 1. What is Java bytecode? Where does bytecode come from? 2. What are the eight Java primitive types? 3. Explain the difference between an object and a class. 4. Explain the difference between the = symbol in Java and mathematics. 5. If a and b refer to different objects of class BankAccount containing methods void deposit(double amount)and double getblance(), consider the following instructions. a.deposit(b.getblance()); b.deposit(a.getblance()); Are the balances of a and b now identical? Explain. 6. What is the difference between a while statement and an if statement. 7. What is wrong with the following loop? double[] data = new double[10]; for (int i = 1; i <= 10; i++) data[i] = 2 * i; Explain two ways of fixing the errors. 8. Consider the following problem description: Employees receive their biweekly pay cheques. They are paid their hourly rates for each hour worked; however, if they worked more than 40 hours per week, they are paid overtime at 200% of the regular wage. What classes should you use to implement it? Explain. 9. How do you discover syntax errors in a program? How do you discover logic errors in a program? 10. What do the following statements print? a) System.out.println( 2 + 3 ); b) System.out.println(2 + 3 );

SECTION B (Answer ALL parts) (a) Write a class ChristmasTree whose tostring method produces a string depicting a Christmas tree like this: (Hint: remember to use escape sequences.) (6 marks) (b) The following class BankAccount is incomplete; some methods are not implemented. Complete the class by implementing four methods: getaccountnumber, deposit, withdraw, and getblance based on the specification described in comments. public class BankAccount { private int accountnumber; private double balance; Constructs a bank account with a zero balance @param anaccountnumber the account number for this account public BankAccount(int anaccountnumber) { accountnumber = anaccountnumber; balance = 0; Constructs a bank account with a given balance @param anaccountnumber the account number for this account @param initialbalance the initial balance public BankAccount(int anaccountnumber, double InitialBalance) { accountnumber = anaccountnumber; balance = initialbalance; Gets the account number of this bank account. @return the account number public int getaccountnumber() { fill in your code

Deposits money into the bank account. @param amount the amount to deposit public void deposit(double amount) { fill in your code Withdraws money from the bank account. @param amount the amount to withdraw public void withdraw(double amount) { fill in your code Gets the current balance of the bank account. @return the current balance public double getbalance() { fill in your code (c) A line in the two-dimensional plane can be specified in various ways: by giving a point (x,y) and a slope m by giving two points (x 1,y 1 ) and (x 2,y 2 ) as an equation in slope-intercept form y = mx + b as an equation x = a if the line is vertical Implement a class Line with four constructors, corresponding to the four cases above. (Hint: Three variables, slope, intercept, and isvertical, should be used to describe the properties of a line in this class.) (6 marks) (d) What is the output from the following programme? List all printed results in order. class errhandling { public static void main(string args[]) { int numer[] = { 4, 8, 64, 128 ; int denom[] = { 2, 0, 0, 32 ; for(int i=0; i<numer.length; i++) { try { System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!");

SECTION C (Answer ONE question only from this section) C1. (a) Given three Java methods, and a method static void inputguest(); static void saveregister(); static void restoreregister(); static char getcommand() for prompting the user to type a character followed by RETURN and reading and returning the next character that is typed, write a Java method static void performprocessrepeatedly() which will behave as follows: It will repeatedly read a character and if the character is i it will execute inputguest() if the character is s it will execute saveregister() if the character is r it will execute restoreregister() if the character is e then the process will immediately terminate. (10 marks) (b) Answer the following questions about the method you have written in part (a) (i) (ii) (iii) (iv) How user friendly is your method when a character other than one of the four valid commands is typed? What could you do (or what have you already done) to give a helpful response in such cases? How would you alter your software to allow other commands (invoking other functions) and to treat uppercase characters as alternatives for lowercase commands, e.g. I instead of i? What feature of JAVA allows us to write robust software that will not crash when incorrect input is provided? Illustrate your answer by showing how the function getcommand() might be designed. performprocessrepeatedy() is an example for repeatedly selecting and performing one of several optional tasks on the basis of choices made by the user, which can be viewed as a design pattern. How often do you expect such a pattern to be used in computer programs for real applications? Justify your answer.

C2. (a) Explain the concept of a 1-dimensional array in Java and illustrate how such arrays are declared and how their elements are accessed. In Java, arrays are examples of objects and are referenced via addresses. Explain why this causes difficulty both with assignment and comparison for equality. What is the difference between the size (or length) of an array and the number of items which it is used to store. How can the size of an array be calculated? (b) Given the following declarations final static int terminator = 999; final static int MaxSize = 100; static int[] Seq = new Seq[MaxSize]; static int n = 0; and a method, which does not raise any exceptions, static int GetInt() which skips input that cannot be parsed as an integer and returns the value of the next valid integer typed by the user. Write a segment of Java code to read a sequence of integers different from and terminated by terminator, and to store them in Seq[0], Seq[1],.., Seq[n-1] respectively. The value of n, which is the number of items in the sequence, is to be calculated as part of the process of reading the sequence. Assume that the user will not type more than MaxSize integers. (6 marks) (c) How would your Java code react to an attempt to enter more than MaxSize integers? How could you alter your code to avoid such reaction? What do you regard as the best way to warn the user that too much data is being entered? Justify your choice.