CS2 Practical 1 CS2A 22/09/2004

Similar documents
CS2 Practical 2 CS2Ah

CS2 Practical 6 CS2Bh 24 January 2005

CSE143X: Computer Programming I & II Programming Assignment #9 due: Monday, 11/27/17, 11:00 pm

2018 Pummill Relay problem statement

Computer Science 1 Ah

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Midterm Exam 2 CS 455, Spring 2011

a b c d a b c d e 5 e 7

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Assignment 5: MyString COP3330 Fall 2017

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Name CIS 201 Midterm II: Chapters 1-8

CPSC 217 Assignment 3

COP Programming Assignment #7

CMPSCI 187 / Spring 2015 Hangman

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CSE 5A Introduction to Programming I (C) Homework 4

CSE 143 SAMPLE MIDTERM

CS 10, Fall 2015, Professor Prasad Jayanti

Project 1: Implementation of the Stack ADT and Its Application

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Csci 102: Sample Exam

General Instructions. You can use QtSpim simulator to work on these assignments.

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Exercise 4: Loops, Arrays and Files

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

COS 226 Algorithms and Data Structures Fall Midterm

Final exam. CS 2110, December 15, 2016, 9:00AM

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

ASSIGNMENT 5 Objects, Files, and More Garage Management

CS24 Week 4 Lecture 2

CS 314 Midterm 2 Fall 2012

More Data Structures (Part 1) Stacks

Soda Machine Laboratory

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

EECE.2160: ECE Application Programming

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

CSE 331 Final Exam 3/12/12

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Lesson 7A Arrays. By John B. Owen All rights reserved 2011, revised 2014

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

You must bring your ID to the exam.

EECE.2160: ECE Application Programming

CMSC 341 Lecture 7 Lists

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Midterm Exam 2 CS 455, Fall 2013

Programming Assignment 2

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Largest Online Community of VU Students

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

MATH 1MP3 Homework #4 Due: 11:59pm, Wednesday, March 6.

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 10, 2005

CIS Fall Data Structures Midterm exam 10/16/2012

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

Introduction to Programming (CS112): Sample

CSC-140 Assignment 5

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

CSIS 10B Lab 2 Bags and Stacks

Engr 123 April 25, 2018 Final Exam Review. 3. Write a method which will accept a string and return the number of three-letter words in the string.

CPSC 221: Programming Project 1

Data Structures And Algorithms

Algorithms & Datastructures Laboratory Exercise Sheet 1

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

CS143 Final Spring 2016

School of Informatics, University of Edinburgh

Project 1: Scheme Pretty-Printer

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Basic Data Structures

ASSIGNMENT 5 Objects, Files, and a Music Player

CS143 Final Fall 2009

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

CSCI 1301: Introduction to Computing and Programming

CS 307 Final Fall 2009

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Programming Assignment 3 Due Monday, March 24 (Note the change in the due date)

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Transcription:

CS2 Practical 1 Basic Java Programming The purpose of this practical is to re-enforce your Java programming abilities. The practical is based on material covered in CS1. It consists of ten simple programming exercises, which you should be able to solve without much effort (the exercises do vary in difficulty). It is crucial that you write code that compiles and runs without producing any errors and that EXACTLY meets the specifications. Your code should not output anything in addition to the specified output. Your submissions will be marked by an automatic marking program. The program will fail any submissions that do not behave exactly as required. It is important to realise that you only get marks for code that compiles and runs correctly; this will be the case for all practicals in CS2 (although marking arrangements for the rest of the practicals will be different). The practical consists of three parts. Part A contains four problems, and Parts B and C each contains three problems. Each of the ten problems is worth 10 marks. The solution to each of the problems consists of one Java file (which you must create from scratch; there are no templates associated with this practical). The name of this file is specified at the end of the problem; take care that the file you submit has exactly the right name. To submit your solution to one of the problems, use the command handin P1 for example filename handin P1 Add.java The deadlines for submitting the problems in each of the three parts are: Wednesday 29 September, 11.00am for Part A, Monday 4 October, 11.00am for Part B, Monday 11 October, 11.00am for Part C. Of course, you may submit your answers to some or all of the problems earlier. The total mark out of 100 is also allocated a grade according to the University marking scale:! This is the first of the four practicals in CS2A. Each practical is worth equal credit, and the four together make up 25% of the final mark for CS2A. Remember that you will need at least a grade C (i.e. at least 50%) in your final CS2 mark 1

( CS2 Practical 1 CS2A 22/09/2004 to proceed to any of the Computer Science or Software Engineering single or joint Honours degree courses. You should also bear in mind the guidelines on plagiarism, which can be found via a link on the CS2 Web page. Note: Programs should send output to standard output (stdout), and not to the standard error stream (stderr). Part A Problem 1: Addition Write a Java program Add which takes two integers as command line arguments and outputs the sum of these integers. For example, the command java Add 3 5 should generate the output: 8 Trailing spaces and new-lines after the number is outputted will not cause the test to fail. The file you submit should be called Add.java. Problem 2: Latin Square A Latin square of size " is an "$#" array of numbers where each of the numbers %'&( )* (+"-, appears only once in each row and column. The easiest )* way to produce such a square is for the first row to be & " and for each row after that to be obtained by cycling the previous row by one place to the left. Write a program Latin which takes an integer as a command line argument and outputs such a Latin square of size specified by the integer. For example, the command java Latin 10 2

should produce the output: 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 1 3 4 5 6 7 8 9 10 1 2 4 5 6 7 8 9 10 1 2 3 5 6 7 8 9 10 1 2 3 4 6 7 8 9 10 1 2 3 4 5 7 8 9 10 1 2 3 4 5 6 8 9 10 1 2 3 4 5 6 7 9 10 1 2 3 4 5 6 7 8 10 1 2 3 4 5 6 7 8 9 Leading and trailing empty lines will not cause the test to fail, nor will spaces at the beginnings and ends of lines. Within each line, there should be just one space character between adjacent numbers. The file you submit should be called Latin.java. Problem 3: Broken Stack The following Java class IntStack implements a Stack data structure for storing integers. It supports the following public methods: IntStack() (constructor): Creates a new empty stack. isempty(): Returns false if the stack stores at least one element and true otherwise. push(int elt): Inserts elt into the stack. pop(): Returns the element of the stack that has been inserted most recently. If the stack is empty, it throws an EmptyStackException. public class IntStack { private class StackItem { int elt; StackItem next; public StackItem(int elt, StackItem next) { this.elt = elt; this.next = nxt; private StackItem top; public IntStack() { top = null; 3

public boolean isempty() { return ( top == null ); public void push(int elt) { top = new StackItem(elt,top); public int pop() throws java.util.emptystackexception { int elt = top.elt; top = top.next; if ( isempty() ) throw new java.util.emptystackexception(); else return elt; Unfortunately, the implementation is faulty. Correct it and submit the corrected code. You may obtain an online copy of the above faulty code from the CS2A Practicals page. The file you submit should be called IntStack.java. Problem 4: Letter Diamonds Write a program Diamond which takes a small integer as a command line argument and produces a letter diamond of that size. For example, the command java Diamond 4 should produce the following output: a b c d e f g h i j k l m n o p After the letters a - z have been used, the program should continue using the letters A - Z, and then a - z and so on. 4

The line with just the a character should be the first line of the output, and the longest line should be left-justified. Within each line, there should be just one space between adjacent letters. Leading and trailing empty lines will not cause the test to fail, nor will spaces at the ends of lines. Incorrect spacing at the beginnings of lines will, however, cause the test to fail. The file you submit should be called Diamond.java. Part B Problem 5: Random Pairs Write a program Pairs which takes a list of names from a file specified as a command line argument, with one name per line of the file, and groups them into random pairs, outputting the allocation to standard output. The elements of each pair should be separated by the word and, and if there are an odd number of names, the last remaining name should be output on its own. For example, the following input: Rob Lucy Rebecca Peter Jane should produce output such as: Rob and Rebecca Peter and Lucy Jane (You need not take account of gender in the way you pair people up!) This exercise will be marked by running the program several times and checking that the output each time is a pairing of the provided names, and that it doesn t produce the same output every time it is run. The file you submit should be called Pairs.java. Problem 6: a Extraction Write a Java program AWords that reads a text file, extracts all words containing the letter a (both lowercase and uppercase a s count) from the text, and prints them to the standard output in the order in which they appear. If the same word appears several times in the text, it should be printed several 5

times. The name of the text file is given to the program as a command line argument. For example, suppose we have a text-file foo that contains the following text: AWords is a program that finds all words containing the first letter of the alphabet. Then the command java AWords foo should generate the output AWords a program that all containing alphabet The tests will not involve any punctuation symbols. The test files only contain alphanumeric characters, spaces and new-lines. The file you submit should be called AWords.java. Problem 7: Counting Distinct Words Write a Java program UniqCount which reads a text file containing a list of words in alphabetical order and writes to standard output the number of distinct words in the file. The file may contain more than one word on each line. The name of the text file is given to the program as a command line argument. For example, suppose we have a text-file words that contains the following text: consecutive dual dual multiple multiple multiple unique unique word Then the command java UniqCount words should generate the output 5 6

. &. # 8 / & : # 8 / # & : # 0 CS2 Practical 1 CS2A 22/09/2004 as there are five distinct words: consecutive, dual, multiple, unique and word. The test files will only contain alphanumeric characters, spaces and newlines. Trailing new-lines after the number is outputted will not cause the test to fail. The file you submit should be called UniqCount.java Part C Problem 8: Matrix Multiplication Write a Java class MatrixMult with a single public method: public int[][] multiply( int[][] A, int[][] B ) This method should multiply the integer matrices and and return the matrix product #. You may assume that and are square matrices of the same size. Here A[i][j] is the item in the i-th row and j-th column of the matrix A, and similarly for B and the result. The following is an example of a matrix multiplication: / &10. /2 03 &4# 657 =5 #98 /2 : #>8 /? :@ &!# ;5< ;5 The file you submit should be called MatrixMult.java. 03. A & Problem 9: Collections Write a Java class SortIntColl with a single public static method: public static Object[] sort(collection c) As the name says, a Java Collection is an object that contains a collection of other objects. 1 The method SortIntColl.sort expects a collection of Integer objects as input and returns an array that contains all Integers in the collection sorted in increasing order. The file you submit should be called SortIntColl.java. Hint: Use the method toarray() of the Collection interface and the method sort(object[]) of the Arrays class. See the Java API Specification for details. 1 Actually, Collection is just a Java interface that is implemented by various classes such as LinkedList. 7

Problem 10: Permutations (Harder!) Write a Java program Permutations which takes a word on the command line and outputs all permutations of the letters, one per line. Thus, the command java Permutations cat should output the following strings in some order: cat cta act atc tca tac Your program will only be tested on words in which none of the letters appear more than once, thus you can choose to include or exclude duplicates. You will probably find it easier to write a program that includes duplicates. Leading and trailing empty lines will not cause the test to fail, nor will spaces at the beginnings and ends of lines. The file you submit should be called Permutations.java. John Longley 8