EASY

Similar documents
Subject: Computer Science

The little book of programming challenges

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Chapter 7 Arrays. One-Dimensional Arrays. Fred Jack. Anna. Sue. Roy

Variables and Data Representation

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS

APCS-AB: Java. Recursion in Java December 12, week14 1

2. Write a program to convert 69F into its equivalent centigrade temperature.

Catalan Numbers. Table 1: Balanced Parentheses

6 th Grade Enriched Math to 7 th Grade Pre-Algebra

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Chapter 1 Histograms, Scatterplots, and Graphs of Functions

Algorithm Design and Recursion. Search and Sort Algorithms

//If target was found, then //found == true and a[index] == target.

Find terms of a sequence and say whether it is ascending or descending, finite or infinite Find the next term in a sequence of numbers or shapes

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Born in Tulsa in 1914 and passed away in Norman in 2010.

NMC Sample Problems: Grade 8

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

LOOPS. Repetition using the while statement

Year 2 Spring Term Week 5 to 7 - Geometry: Properties of Shape

Section 1.4 Proving Conjectures: Deductive Reasoning

Introduction to Computer Science Unit 3. Programs

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

B. V. Patel Institute of Business Management, Computer & Information Technology, UTU 2013

Chapter Goals. Contents LOOPS

9 abcd = dcba b + 90c = c + 10b b = 10c.

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

SECTION 5.1. Sequences

LAB 13: ARRAYS (ONE DIMINSION)

Animations involving numbers

Practical List of. MCA IV SEM Session -2010

[ the academy_of_code] Senior Beginners

The type of all data used in a C++ program must be specified

Andrew Shitov. Using Perl Programming Challenges Solved with the Perl 6 Programming Language

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Recursion Chapter 3.5

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Practice Problems for the Final

BB4W. KS3 Programming Workbook INTRODUCTION TO. BBC BASIC for Windows. Name: Class:


Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

Mill Hill School. 13+ Entrance Examination Mathematics. January Time: 1 hour. School: Mark: Materials required for examination

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

PROGRAMMING IN C AND C++:

Hanoi, Counting and Sierpinski s triangle Infinite complexity in finite area

WASHINGTON STATE MIDDLE SCHOOL COMPUTER SCIENCE COMPETITION 2017

CMPSCI 187 / Spring 2015 Hangman

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

AP Computer Science Unit 3. Programs

GCSE Computer Science Component 02

Counting Problems; and Recursion! CSCI 2824, Fall 2012!

Introduction to Python Programming

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

(Refer Slide Time: 1:27)

JMC 2015 Teacher s notes Recap table

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

The Pip Language Reference Manual. PLT (W4115) Fall Frank Wallingford

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Problem A - EPIC. Line Width of Letters (# characters) EEEEEEEEEE EEEEEEEEEE EE EE EEEEEEEEEE EEEEEEEEEE EE EE EEEEEEEEEE EEEEEEEEEE

WASHINGTON STATE MIDDLE SCHOOL COMPUTER SCIENCE COMPETITION 2017

Assignment 2.4: Loops

7. Arrays, More Java Looping

Section 1.2 Fractions

Decimals should be spoken digit by digit eg 0.34 is Zero (or nought) point three four (NOT thirty four).

Functional Programming in Haskell for A level teachers

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

2.1 Basics of Functions and Their Graphs

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

ASSIGNMENT 2. COMP-202A, Fall 2011, All Sections. Due: Monday, October 17th, 2011 (23:30)

Lecture 13: AVL Trees and Binary Heaps

Chapter 6 Programming the LC-3

FROM FRACTIONS TO DECIMALS (V2) WARM UP!

Introduction to Scientific Computing

Conditionals !

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

Bulgarian Math Olympiads with a Challenge Twist

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Properties and Definitions

Analysis of Algorithms. 5-Dec-16

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

1 Lexical Considerations

The Further Mathematics Support Programme

4. Write a sum-of-products representation of the following circuit. Y = (A + B + C) (A + B + C)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Chapter 4 Introduction to Control Statements

9 abcd = dcba b + 90c = c + 10b b = 10c.

Math Circles: Pigeons and Rams(ey)

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

CTI, November 19, 2015

Lesson 1. Why Use It? Terms to Know

Analysis of algorithms

Transcription:

Downloaded from: justpaste.it/hsfm ------------------------------- EASY ------------------------------- NUMBERS Even or Odd Write a program that accepts a number and returns whether the number is equal or odd. Odd number x times Write a program that prints out odd numbers for x amount of times. Rolling a die Write a program which emulates rolling a die. Every time the program is run, it outputs a random number between 1 and 6. Multiples Write a program UserEntersMultipleNew.java where the user enters the multiple and the number. So if the user enters 3 and 7 the output will be 0 7 14 and if the user enters 4 and 101 the output would be 0 101 202 303 Times table Write a program SimpleTimesTable.java, such that if the user enters two numbers m and n, the program prints out the first m items in the times table for n. For example if the user enters 4 and 5, the program outputs 1 times 5 = 5 2 times 5 = 10 3 times 5 = 15 4 times 5 = 20 and if the user entered 3 and 19 the program outputs 1 times 19 = 19 2 times 19 = 38 3 times 19 = 57 Multiplication table Write a program to print out all the times tables. The user should enter how many tables and also how many to go up to for each table. For example: How Many Numbers for each table? 7

Up to which times table 3 1 times 1 = 1 2 times 1 = 2 3 times 1 = 3 4 times 1 = 4 5 times 1 = 5 6 times 1 = 6 7 times 1 = 7 1 times 2 = 2 2 times 2 = 4 3 times 2 = 6 4 times 2 = 8 5 times 2 = 10 6 times 2 = 12 7 times 2 = 14 1 times 3 = 3 2 times 3 = 6 3 times 3 = 9 4 times 3 = 12 5 times 3 = 15 6 times 3 = 18 7 times 3 = 21 Factorial Write a program that accepts a number and prints its factorial. The factorial of n is 1 times 2 times... times n. The factorial of 0 is 1. Do the same but with recursion. Fibonacci Write a program that accepts a number and returns fibonacci sequence. Do the same but with recursion. Lowest to highest Write a program that acceepts x amount y numbers and prints them in order from lowest to higest and from higest to lowest. Largest, smallest, sum and average Write a program that accepts x amount of y numbers and prints out largest, smallest, sum and average without using arrays. Then do the same with arrays and make a new class for the methods. Backwards Write a program that accepts x amount of y numbers and prints them out in reverse order. Backwards - arguments Same as before, but use command line arguments as input. Multiplication with addition Suppose Java doesn't have multiplication. Write a program that multiplies x * y without using * but using

addition instead. Exponential with addition Same exercise, but do exponential this time. Guessing game - Human Write a program that implements the following game: The computer has thought of a number (245). The computer says: Try to guess the number I m thinking of: The user must try to guess this number by typing in a number. If the number the user types in is less than the number the computer thought of, it says: too low - guess again: and repeats the process. If the number the user types in is greater than the number the computer thought of, it says: too high - guess again: and repeats the process. If the user guesses the number correctly, the computer says: Correct! The number of guesses you made was... and the program finishes. This is the only way to finish the program. So if the user never guesses correctly, the game should go on for ever! Leap year Write a program that accepts a number and returns whether it is leap year or not. WORDS Dictionary order Write a program that accepts three words and prints them out in dictionary order and then in reverse dictionary order. Make it case insensitive. Unicode Write a program that prints out all Unicode values. Longest string Write a program that accepts x amount of words and then prints out the longest one. Occurances of a character in a string Write a program that accepts a string and a character and returns how many times can that character be found in a word. Backwards - arguments Write a program that allows you to have as many arguments as you want and prints them out in reverse. Reading a file Write a program that reads the content of a file line by line. Do the same but char by char. Writing to a file Write a program that writes to a file until the user presses enter. ------------------------------- HARD -------------------------------

NUMBERS Shapes Write a program that prints the following shapes. Square/Rectangle **** *** **** Top left triangle Upside down triangle Diamond * Guessing game - Computer Bottom left triangle Top right triangle Left romboid * * Bottom right triangle Normal triangle Right romboid * * Write a program that tries to guess the number thought of by the user. The number is between 0 and 1000. If the computer s guess is too high, the user should enter 2. If the computer s guess is too low, the user should enter 1. If the computer s guess is correct, the user should enter any integer except 1 or 2. If the computer takes more than ten guesses then the computer loses otherwise the user loses. Occurrences - number Write a program that accepts x amount of y numbers and then asks the user which number they want to check. It then prints how many times has that number occur in the array. Multiplication Write a program that multiplies numbers with addition using recursion. Exponentiation Write a program that calculates exponent of a number using recursion (one multiplication operation allowed). Syracuse sequence

The Syracuse Sequence starting with 14 goes like this: 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 The rule is as follows: if n is even then the next number in the sequence is n/2 and if n is odd the next number is 3n + 1. The sequence stops at 1. Using recursion, write a program not containing the word while, such that for all positive integers, n, java syr n prints out the Syracuse sequence starting with n. (Try running syr.class to see how your program should behave.) An interesting fact about the Syracuse sequence is that nobody knows whether it always ends with 1 or not! No-one has proved it and no one has found an example that does not end in 1. How old are you Write a program that accepts DOB in three numbers and returns the age. Hangman Write a program that accepts a word and then prints ------- and asks the user to enter a character. If the character is in the string, return --d---- and so on. Limit this to ten guesses. Roman Numerals Write a program that accepts a number in roman numerals and returns arabic numerals. So MCMXCIX would return 1999. Shuffling a pack of cards Write a program that shuffles 52 cards randomly. Your program should output the 52 having been shuffled. You may assume that the cards are numbered 1 to 52. The way I did it was as follows: Create an array a of 52 integers. For each i store i at position i in the array. Generate a random number k between 1 and however many cards left in the array (use java.util.random ). Print out a[k]. Then move all the elements of the array which are to the right of k one to the left. (a[i] = a[i + 1]), in effect deleting a[k]. Subtract one from the total number of cards left in the array. Repeat this 52 times. Noughts and crosses Write a program that allows two people to play noughts and crosses on the computer. Your program should stop illegal moves and detect when the game is over and output who has won. Noughts and crosses 2 This time, the computer plays against the user. The user should be allowed to go first and the computer must respond each time with a random legal move. The way I have done this is very crude. For the computer s move I repeatedly generate a random number between 1 and 9 until I get a free square. That s the move the computer makes. But this time if the computer spots an immediate win it goes for it! Nims The computer plays against the user. Implement a winning strategy. In nim we have n piles of matches. The user and the computer take it in turns picking up matches. The rules are that on each go you can pick up as many matches as you like from exactly one pile. The person who is left with no matches is the loser. Mastermind Implement the well-known game with coloured pegs. You can use digits instead of colours. The computer s pattern of n digits is hard-wired into the program. The user tries to guess the pattern. The computer responds, telling the user two values: 1. how many digits are right and in the right place (the black pegs) 2. how many digits are right but in the wrong place (the white pegs) Carry on until the user gets it. Tell the user how many guesses it took.

Array of Ints Write a program that accepts an array of ints, sorts them in either ascending or descending and has a function to search the array for an integer. n-th line from a file Write a program that returns a line from the file. User should enter which line to print. Odd lines Write a program that prints out odd lines from a file. WORDS Occurrences - word Write a program where the user types in a number of Strings stored in an array of Strings and then the program prints out all the Strings that have the most occurrences of the character. Print words in reverse order Write a program that accepts x amount of string arguments and prints them out in reverse order. Counting the number of lines, words and chars Write a program that opens a file passed as an argument and counts how many lines there are. Do the same but count words and chars instead. These are three separate programs, then do one that does everything. Swapping all 'a' with 'b' Write a program that opens a file passed as an argument, reads the content and swaps all 'a' with 'b' letter. Array of Strings Write a program that accepts an array of strings, sorts them in either ascending or descending and has a function to search the array for a string. Shuffling a pack of cards Write a program that shuffles 52 cards randomly. Your program should output the 52 having been shuffled. This time you must output Strings like five of clubs or ace of spades instead of just a number. I used two arrays to store the names values and suits of cards: String [ ] val = {"ace","two","three","four", "five","six","seven","eight","nine", "ten", "jack", "queen","king"}; String [ ] suit ={"clubs","diamonds","hearts","spades"}; We then need a way of converting numbers from 1 to 52 into these. To do this I used arithmetic; dividing by thirteen for the suit and taking the remainder for the value. Spell-checker 1. Find out about the Soundtex algorithm. 2. Write a method that implements it. 3. Write a spell-checker that goes through a file and every time it finds a word not in the dictionary it offers the user a list of similar words using Soundtex to choose from. It prompts the user either to accept the word or to enter a replacement. New words entered by the user should be stored in a local dictionary. Simple spell-checker There is a file Lecture11/words consisting of a big list of words in English (courtesy of Unix), one per line. We can use it to write a simple spell checker. We are using the method

java.lang.string.startswith(). The user simply types the beginning of the word he wants to check as a command line argument. For example, java spell freq will print out all English words starting with freq. These are: frequencies frequency frequent frequented frequenter frequenters frequenting frequently frequents Slightly better spell-checker Rather than expecting the user to enter the word they are looking for on the command line, we can give the user a prompt > to enter a word. The program keeps giving the user another go. The program stops if the user simply presses return. n-th char from a file Write a program that prints out n-th char from a file. Odd chars from a file Write a program that prints out all characters that have odd unicode value. Manycat Write a program called manycat.java that is the same as cat.java except many files can be printed out one after the other. For example to print fred1, fred2, and fred3 we would type java manycat fred1 fred2 fred3. Swapcase Write a program called swapcase.java that is the same as cat.java except that all lower case letters are swapped for upper case and vice versa. ASCII Write a program called ascii.java that is the same as cat.java except that as well as printing out each character, it also prints its Unicode value. (See page 169 Number 49 for the answer.) What is the Unicode value for the newline character? Remnewline Write a program called remnewline.java that is the same as cat.java except that it doesn t print out the newline characters in the file. Tenperline Write a program called tenperline.java that is the same as remline.java except that it prints 10 characters on each line. List Words Write a program that prints out every word in a file, one word per line.

Assignment Spell Checker Write a spell checker that goes through a file and prints out all the words it finds that aren t in the dictionary. Hard Assignment A Better Spell Checker Write a spell checker that goes through a file and every time it finds a word not in the dictionary it prompts the user either to accept the word or enter a replacement. Anagrams Here is a program that might be useful for people who do crossword puzzles. The user types in a word and it finds all the anagrams of the word in the English dictionary. An anagram is simply a re-arrangement, for example taste is an anagram of state. The program is based very closely on the spell checker spell1.java. The only difference is that instead of checking whether each word in the dictionary starts with the word the user entered, we check whether the word is an anagram of the word in the dictionary. We have written a method called anagram which returns true if s and t are anagrams of each other. We do this by converting s and t to arrays of characters, as and at. Then, for each element of at, we see if we can find it in as. If we do, we change the corresponding element of as to zero, so we never find it again. A zero is not a printable character. If at any stage we don t find a letter we are looking for, then it can t be an anagram. Diary program A diary consists of a number of events. Each event has three associated bits of information. 1. The date when the event takes place. 2. The number of days before they wish to be reminded of the event. 3. The text of the event. You must write a system that gives the user the following choices: 1. Add an event. 2. See all today s events (you must find today s date). Each event must be displayed one at a time, each time asking the user if they want to: (a) Delete the event. (b) Change the date of the event. (c) Change the number of days before they wish to be reminded of the event. (d) Continue. 3. See all today s reminders.each event must be displayed one at a time, each time asking the user if they want to: (a) Delete the event. (b) Change the date of the event. (c) Change the number of days before they wish to be reminded of the event. (d) Continue. 4. See all events on a particular day. Each event must be displayed one at a time, each time asking the user if they want to: (a) Delete the event. (b) Change the date of the event. (c) Change the number of days before they wish to be reminded of the event. (d) Continue.

5. See all events on a particular time interval (forget leap years). Each event must be displayed one at a time, each time asking the user if they want to: (a) Delete the event. (b) Change the date of the Event. (c) Change the number of days before they wish to be reminded of the Event. (d) Continue. 6. See all Events, each time asking the user if they want to: (a) Delete the Event. (b) Change the date of the Event. (c) Change the number of days before they wish to be reminded of the Event. (d) Continue. 7. Update the diary file. 8. Exit the system. You must design a Diary class and an Entry class. Your Diary class should contain a Vector of Objects of type Entry. Your diary should be stored in a file. This file should be read into memory when you start the system and it should be updated when you leave the system. All changes should be done in memory. You need three classes: A Date class with three fields: int day int month int year An Event class with 3 fields: String text Date date int reminder A Diary class with 2 fields: Person owner Vector events -------------------------- GRAPHICAL -------------------------- Mouse Motion Write a Java program which displays a small square of a colour of your choice. The left hand corner of the square must have one co-ordinate equal to the day of the month you were born, the other co-ordinate of the left hand corner must be the integer corresponding to the month you were born (Jan=1, Feb=2, etc.). The side of the square must correspond to your age in years. The square must change to a different colour when the mouse is inside it and back to the original colour when the mouse is not inside it. The program must keep responding to the mouse in this way. You should use the element package and the Drawing window described in Chapter 3. Your program should contain an infinite loop, so it goes on for ever. You will probably use the following methods from the element package:

getmouse() contains setforeground fill You will need to work out some boolean expressions to test whether the mouse is inside the square that you have drawn. See the contains method for this. Maze Write a Java program that represents a maze. The maze must have a start and a finish. The idea is to move the mouse from the start to the finish without going outside the maze. The program should give an error message if the mouse goes off the path of the maze and force the user to start again by ending the program. If the user gets from the start to the finish successfully the program should display to the user how long it took in seconds. First make a simple shape for the maze. Mine was like this: DrawingWindow d = new DrawingWindow(500,500); Text s = new Text("start"); Text f = new Text("end"); s.center(new Pt(250,400)); f.center(new Pt(255,200)); Circle start= new Circle(250,400,30); Rect mid1 = new Rect(240,200,10,200); Circle finish= new Circle(255,200,30); d.fill(start); d.fill(mid1); d.fill(finish); d.setforeground(color.white); d.draw(s); d.draw(f); Then have a loop which gets the position of the mouse and checks where it is. Use the contains() method for this. For example, start.contains(p1) will be true if and only if point p1 is inside the start circle. etc. Use long b=system.currenttimemillis(); to get the current time.