Hej!
Here are my solutions on Problem Solving And Programming class assignments, if you have any questions, mail me [email protected]
Do not hesitate to ask your teaching assistant at the practical meetings (or Jonas at the lectures) if you have any problems. You can also post a question in the assignment forum in Moodle.
- Install Java Download and install Java SE JDK: www.oracle.com/technetwork/java/javase/downloads. Also, there are plenty of instruction videos available in YouTube. Just search for "Install Java X" where X is your operating system.
- Install Eclipse Download and install Eclipse IDE for Java Developers: http://www.eclipse.org/downloads/. Once again, there are plenty of instruction videos available in YouTube. Just search for "Install Eclipse X" where X is your operating system.
- Setup Eclipse Workspace Before you start programming, do the following.
- Create an Eclipse workspace (a folder) with the name java_courses on some location in your home directory.
- Create a Java project with the name 1DV506 inside the workspace.
- Create a package with the name YourLnuUserName_assign1 inside the project. For example, it might look something like wo222ab_assign1.
- Save all program files from the exercises in this assignment inside the package YourLnuUserName_assign1.
- In the future: create a new package (YourLnuUserName_assignX) for each assignment and a new project (with the course code as name) for each new course using Java. 4.Edit, compile and execute. Create, compile and execute the following program inside your assignment 1 package:
/* The classical "Hello World!" program. */ public class Hello {
public static void main(String[] args) { System.out.println("Hello World!"); // Print }
}
Write a program Print.java, which will print the phrase Knowledge is power!
- on one line,
- on three lines, one word on each line,
- inside a rectangle made up by the characters = and |.
Write a program Quote.java which reads a line of text (using class Scanner ) and then prints the same line as a quote (that is inside " "). An example of an execution:
Write a line of text: I wish I was a punk rocker with flowers in my hair.
Quote: "I wish I was a punk rocker with flowers in my hair."
Write a program Seconds.java which reads three integers (hours, minutes, seconds) and then computes the corresponding time measured in seconds. For example, 1 hour, 28 minutes and 42 seconds is equal to 5322 seconds. An example of an execution:
Hours: 1 Minutes: 28 Seconds: 42 Total time measured in seconds: 5322
Write a program BMI.java which computes the BMI (Body Mass Index) for a person. The program will read length and weight from the keyboard and then present the result as output. The BMI is computed as weight/(length)^2, where the length is given in meters and the weight in kilograms. An example of an execution:
Give your length in meters: 1,83
Give your weight in kilograms: 83
Your BMI is: 25
Note: the BMI is always an integer.
Write a program Time.java, which reads a number of seconds (an integer) and then prints the same amount of time given in hours, minutes and seconds. An example of an execution:
Give a number of seconds: 9999
This corresponds to: 2 hours, 46 minutes and 39 seconds.
Hint: Use integer division and the modulus (remainder) operator.
Write a program SumOfThree.java which asks the user to provide a three digit number. The program should then compute the sum of the three digits. For example:
Provide a three digit number: 483
Sum of digits: 15
If Time Permits
Exercise 11 is marked as VG task ==> only mandatory for those of you that aspire for a higher mark (A or B).
Write a program Change.java that computes the change a customer should receive when he has paid a certain sum. The program should exactly describe the minimum number of Swedish bills and coins that should be returned rounded off to nearest krona (kr). Example:
Price: 372.38
Payment: 1000
Change: 628 kronor1000kr bills: 0
500kr bills: 1
100kr bills: 1
50kr bills: 0
20kr bills: 1
10kr coins: 0
5kr coins: 1
1kr coins: 3
Write a program Convert.java, which reads a temperature in Fahrenheit and then converts it to Celsius using the formula:
C = (F-32)*5/9
The result should be presented with a single decimal correctly rounded off.
Write a program ShortName.java, reading a first name and a last name (given name and family name) as two Strings. The output should consist of the first letter of the first name followed by a dot and a space, followed by the first four letters of the last name. An example of an execution:
First name: Anakin
Last name: Skywalker
Short name: A. Skyw
Hint: Use methods of the String class. What will happen if the last name consists of less than four letters?
Write a program TelephoneNumber.java, generating and printing a random telephone number of the form 0XXX-ZYYYYY. The area code consists of a zero followed by three digits (X). The local number can not start with a zero (Z), all other digits (Y) are random.
Hint: Use the class java.util.Random.
Write a program Distance.java which reads two coordinates in the form (x,y) and then computes the distance between the points, using the formula
distance = Sqrt( (x1-x2)^2 + (y1-y2)^2 )
Sqrt() means "the square root of" and ^ means "raised to". The answer should be presented with three decimal digits.
Hint: Use the class java.lang.Math for the computations.
If Time Permits
Exercise 16 is marked as VG task ==> only mandatory for those of you that aspire for a higher mark (A or B).
Write a program WindChill.java that asks the user for a temperature (°C) and the wind speed (measured in m/s) and then computes the so-called wind chill temperature using Siple's formula. For example:
Temperature: -7.8
Wind speed: 8.4
Wind Chill Temperature: -24.5
Only exercises 5-16 should be handed in. (Notice that the VG exercises 11 and 16 are not mandatory.) We are only interested in your .java files. Hence, zip the directory named YourLnuUserName_assign1 (inside directory named src) and submit it using the Moodle submission system.
Do not hesitate to ask your teaching assistant at the practical meetings (or Jonas at the lectures) if you have any problems. You can also post a question in the assignment forum in Moodle.
Create a new package with the name YourLnuUserName_assign2 inside the Java project 1DV506 and save all program files for this assignment inside that package.
Exercises 7-9 and 13 are VG tasks that is only needed to do if you aspire for a higher grade (A or B).
The ability to combine different control statements to solve a problem is very important. Therefore, there is quite a large number of (short) tasks on this topic.
Write a program CountA.java that reads a string from the keyboard and then prints how many 'a' and 'A' the string contains. An example of what an execution might look like:
Provide a line of text: All cars got the highest safty grading A.
Number of 'a': 3
Number of 'A': 2
Write a program Backwards.java that reads an arbitrary string from the keyboard and then prints it backwards. An example of an execution:
Provide a line of text: Anakin Skywalker
Backwards: reklawykS nikanA
Notice: You are supposed to use control statements to iterate over the input text backwards.
Write a program LargestK.java, which for any given positive integer N (read from the keyboard) computes the largest integer K such that 0+2+4+6+8+...+K < N. An example of an execution:
Give a positive integer: 25
The largest K such that 0+2+4+6+...+K < 25 => K=8
Write a program HighLow.java, implementing the game High and Low. The computer chooses a random integer between 1 and 100 and lets the user guess the value. After each guess, the user is given a clue of the type “higher” or “lower”. An example of an execution:
Guess 1: 67
Clue: higher
Guess 2: 82
Clue: lower
Guess 3: 77
Correct answer after only 3 guesses – Excellent!
After 10 guesses, the program ends with a proper comment.
Write a program Triangle.java reading a positive odd integer N from the keyboard, and then prints two triangles. First a right-angled triangle, then an isosceles triangle. An example of an execution:
Provide an odd positive integer: 7
Right-Angled Triangle:
*
**
***
****
*****
******
*******
Isosceles Triangle:
*
***
*****
*******
The program should end with an error message if the input N is not an odd postive integer.
Write a program SecondLargest that reads 10 integers from the keyboard and then prints the second largest one. An example of an execution:
Provide 10 integers: 67 -468 36 1345 -7778 0 34 7654 45 -666
The second largest is: 1345
Try to design the program such that changing the number of integers to be read (10) is easy.
Recommendation: Use a smaller value than 10 while developing the program.
Notice: You are not allowed to use arrays or any other data structure for storing all the integers.
Write a program CountDigits.java that for an arbitrary positive integer N (read from the keyboard) prints the number of zeros, odd digits, and even digits. An example of an execution:
Provide a positive integer: 6789500
Zeros: 2
Odd: 3
Even: 2
Notice: We consider 0 to be neither odd nor even.
Write a program BirthdayCandles that computes how many boxes of candles a person needs to buy each year for his birthday cake. You can assume that the person reaches an age of 100, the number of candles used each year is the same as the age, that you save non-used candles from one year to another, and that each each box contains 24 candles. Also, at the end, we want you to print the total number of boxes one has to buy, and how many candles that are available after having celebrated the 100th birtday. An example of an execution:
Before birthday 1, buy 1 box(es)
Before birthday 7, buy 1 box(es)
Before birthday 10, buy 1 box(es)
Before birthday 12, buy 1 box(es)
Before birthday 14, buy 1 box(es)
...
Before birthday 95, buy 3 box(es)
Before birthday 96, buy 4 box(es)
Before birthday 97, buy 5 box(es)
Before birthday 98, buy 4 box(es)
Before birthday 99, buy 4 box(es)
Before birthday 100, buy 4 box(es)
Total number of boxes: 211, Remaining candles: 14
Notice: In our example we only have a print-out of those birthdays where you must buy boxes. In the non-printed years (e.g. 2-6 and 8-9) you can handle the birthdays without having to buy any more candles.
Write a program Palindrome.java, testing if a line of text (read from the keyboard) is a palindrome. A palindrome is a text consisting of the same sequence of characters read backwards, as if read from the front. Ignore all characters that are not letters, and consider an upper case letter to be equal to the corresponding lower case letter. Examples of palindromes:
"Anna" "x" "Ff" "A1 n2%}=3N{[a]" "Was it a rat I saw?”
Hint: The two static methods Character.isLetter(char c) and Character.toLowerCase(char c) might be useful!
Complete the program Reverse.java below such that: It first prints the content of the array text. Reorder the array elements backwards (in opposite order) by shifting place of first and last character, second and second to last character, etc. Then print the array content one more time.
public class Reverse {
public static void main(String[] args) {
char[] text = { '!', 'y', 's', 'a', ' E', ' ', 's', 'a', 'w', ' ', 's', 'i', 'h', 'T' }; // Continue here ...
}
}
Notice: You should not only print them backwards. You should also swap places of the elements in the array.
Create a program ReverseOrder.java that reads an arbitrary number of positive integers from the keyboard and then prints them in reverse order. The reading stops when the user inputs a negative number. An example of an execution: Enter positive integers. End by giving a negative integer.
Integer 1: 5
Integer 2: 10
Integer 3: 15
Integer 4: 20
Integer 5: -7
Number of positive integers: 4
In reverse order: 20, 15, 10, 5
Note: The user is not supposed to give the number of integers to enter.
Write a program FrequencyTable.java that simulates that you are rolling a dice 6000 times. At the same time, keep track of the number of times you get the result 1, 2, .... (Use an array to save the numbers.) After the simulation, present the frequencies for the different numbers. An example of an execution:
Frequencies when rolling a dice 6000 times.
1: 1025
2: 996
3: 980
4: 1006
5: 1035
6: 958
When the union is reporting about the latest salary negotiations they are presenting the average salary, the median salary, and the salary gap for the workers that they represent. Write a program SalaryRevision.java that reads an arbitrary number of salaries (integers) and then reports the median and average salaries, and the salary gap. All of them should be integers (correctly rounded off). By salary gap we mean the difference between the highest and lowest saleries. The median salary is the middle salary (or average of the two middle salaries) when all saleries have been sorted. The easiest way to sort an ArrayList is to use the static sort method in class java.util.Collections.
Two different executions might look like this:
Provide salaries (and terminate input with 'X'): 21700 28200 26300 25100 22600 22800 19900 X
Median: 22800
Average: 23800
Gap: 8300
Provide salaries (and terminate input with 'X'): 22100 29800 27300 25400 23100 22300 X
Median: 24250
Average: 25000
Gap: 7700
All exercises should be handed in and we are only interested in your .java files. (Notice that the VG exercises 7, 8, 9, and 13 are not mandatory.) Hence, zip the directory named YourLnuUserName_assign2 (inside directory named src) and submit it using the Moodle submission system.
Do not hesitate to ask your teaching assistants at the practical meetings (or Jonas at the lectures) if you have any problems. You can also post a question in the assignment forum in Moodle.
Create a new package with the name YourLnuUserName_assign3 inside the Java project 1DV506 and save all program files for this assignment inside that package.
In exercises 1 and 2 below you are supposed to create a number of static methods. They should all be created inside the same class that contains the main method. The main method should work as test program that demonstrates how the different methods in your class can be used.
Create a class Arrays.java that apart from the main method also contains the following static methods:
- Method int sum(int[] arr), adding all elements of the array arr and returning the sum.
- Method String toString(int[] arr), creating a string containing a nice-looking print out of the content of the array. It should be possible to use it in the following way.
int[] n = {3,4,5,6,7}; String str = Arrays.toString(n); System.out.println("n = " + str);
- Method int[] addN(int[] arr, int n), creating and returning a new array, where n have been added to all elements in arr. Array arr should be left unchanged.
- Method int[] reverse(int[] arr), creating and returning a new array, consisting of all elements in arr in reverse order. Array arr should be left unchanged.
- Method boolean hasN(int[] arr, int n), returning true if n is contained in the array arr, false otherwise.
- Method void replaceAll(int[] arr, int old, int nw), replacing all occurrences of old with nw in arr.
- Method int[] sort(int[] arr), returning a new sorted array (increasing order), containing the same set of integers as arr. Array arr should be left unchanged.
- Method boolean hasSubsequence(int[] arr, int[] sub), returning true if the sequence sub is a subsequence of array arr, false otherwise. The case hasSubsequence({1,2,3,4,5}, {3,4,5}) should return true since {3,4,5} is a part of {1,2,3,4,5}. The case hasSubsequence({1,2,3,4,5}, {1,3,5}) should return false since the exact sequence of elements {1,3,5} cannot be found in {1,2,3,4,5}.
Notice: You are supposed to implement all these methods from scratch without any use of the array related methods in the Java library.
Create a class SweID.java that apart from the main method also contains a number of static methods related to the Swedish identity number in the form YYMMDD-NNNN. Information about the structure of Swedish identity numbers can be found at Wikipedia (Wikipedia: Personal identity number (Sweden)).
We expect you to consider each ID number as a single string of type "YYMMDD-NNNN". The class should contain the following static methods:
- Methods getFirstPart and getSecondPart, returning the first part (YYMMDD) and second part (NNNN) of the identity number, respectively.
- isFemaleNumber, isMaleNumber, returning true if the personal identity number belongs to a woman or a man, respectively.
- areEqual, comparing two ID numbers checking if they correspond to the same identity number.
- (VG-exercise) isCorrect, returning true if the number is a correct identity number. To get a passed result you have to check that the date is correct (i.e. the year, month and day should be correct). You must also check that the last digit of the number is correct according to the rules given in the link above.
Feel free to add more methods, if you think anything is missing. Suitable types for arguments and return values are up to you to decide.
Examples:
- 640123-8826 is a correct female number
- 550414-0913 is a correct male number
- 551314-0913 is not correct number (unvalid month)
- 550414-0912 is not correct number (unvalid last digit)
Clarification: all students are supposed to do subtasks 1, 2, and 3. To get the highest grades you must also do subtask 4.
In the exercises below you are supposed to create your own classes. We also want that you, for each class (e.g. MultiDisplay), to create a test program (e.g. MultiDisplayMain) containing a main method that demonstrates how the different methods in your class can be used.
- Exercise 3 (MultiDisplay.java MultiDisplayMain.java)
Create a class MultiDispla that when executed using this code:
MultiDisplay md = new MultiDisplay();
md.setDisplayMessage("Hello World!");
md.setDisplayCount(3);
md.display(); // ==> print-out
md.display("Goodbye cruel world!", 2); // ==> print-out
System.out.println("Current Message: "+ md.getDisplayMessage());
results in the following console print-out:
Hello World!
Hello World!
Hello World!
Goodbye cruel world!
Goodbye cruel world!
Current Message: Goodbye cruel world!
The class MultiDisplay should of course be able to handle other messages and other numbers of display counts.
- Exercise 4(AlarmClock.java AlarmMain.java)
Download and install the class AlarmClock. Then write a program AlarmMain that uses AlarmClock to:
- Set clock time to 23:48
- Display time
- Set alarm to wake up at 6:15
- Let the clock "tick" for 500 minutes
- Display time again
Notice: You are not allowed to make any changes in the AlarmClock class except maybe to change the package name.
- Exercise 5(TextAnalyzer.java TextAnalyzerMain.java)
Create a class TextAnalyzer that when executed using this code:
String text = "My name is Anakin Skywalker. My age is 42."; TextAnalyzer ta = new TextAnalyzer(text);
System.out.println("Char Count: "+ ta.charCount());
System.out.println("Upper Case Count: "+ ta.upperCaseCount());
System.out.println("Whitespace Count: "+ ta.whitespaceCount());
System.out.println("Digit Count "+ ta.digitCount());
if (ta.containsChar('x'))
System.out.println("The text contains char \'x\'");
if (ta.containsString("nakin"))
System.out.println("The text contains substring \"nakin\"");
results in the following console print-out:
Char Count: 42
Upper Case Count: 4
Whitespace Count: 8
Digit Count 2
The text contains substring "nakin"
The class TextAnalyzer should of course be able to handle other texts in a correct way. The methods containsChar and containsString should of course also give a correct result (true/false) for other characters and strings.
- Exercise 6(Point.java PointMain.java)
Create a class Point that when executed using this code:
Point p1 = new Point();
Point p2 = new Point(3,4);
System.out.println(p1.toString()); // ==> (0,0)
System.out.println(p2.toString()); // ==> (3,4)
if (p1.isEqualTo(p2)) // False!
System.out.println("The two points are equal");
double dist = p1.distanceTo(p2);
System.out.println("Point Distance: "+dist);
p2.move(5,-2); // ==> (8,2)
p1.moveToXY(8,2); // ==> (8,2)
if (p1.isEqualTo(p2)) // True!
System.out.println("The two points are equal");
results in the following console print-out:
(0,0)
(3,4)
Point Distance: 5.0
The two points are equal
The class Point should of course be able to handle other points with different (x,y) values. Notice:
- The coordinates (x,y) are always integers.
- The method toString returns a string with coordinates suitable for print-outs.
- Distance between two points is computed in the same way as in Exercise 15, Assignment 1.
- Two points are equal if they have the same coordinates.
- Method move moves the point certain steps in x- and y-direction.
- Method moveToXY provide a new set of coordinates.
This section contains a number of exercises where you are supposed to create you own classes. For each task, we expect a Main class, showing how all methods in the class or classes work. For example, for the class Fraction.java there should be a class FractionMain.java showing how all methods of Fraction.java can be used. All classes are supposed to be commented and follow principles such as encapsulation.
- Exercise 7(Fraction.java FractionMain.java)
Create a class Fraction.java, representing a fractional number of the form N/D, where N (the numerator) and D (the denominator) both are integers. If the denominator is equal to zero, an error message should be printed. The class should include the following members:
- A constructor, creating and initializing a new fractional number.
- Methods getNumerator and getDenominator, returning the numerator or denominator, respectively.
- Method isNegative, returning true if the fractional number is negative.
- Methods add, subtract, multiply, divide, performing the corresponding operations on two fractional numbers and returning a new fractional number. It is up to you to decide a proper way of handling the case when one of the fractional numbers have a zero denominator.
- isEqualTo, comparing two Fraction-instances, checking if they correspond to the same fractional number.
- toString, returning a string representation of the fractional number on the form N/D.
Feel free to add more methods, if you think anything is missing. Suitable argument and return types are up to you to decide.
Extra, voluntary work if you are interested in mathematics: Make sure that the fractional number is in the most simplified form possible. For example, the fractional numbers 2/4 and 35/50 should internally be represented as 1/2 and 7/10. This means that the internal representation always should be the two smallest integers N and D corresponding to the given fractional number. Useful information can be found at Wikipedia: Euclidean algorithm.
- Exercise 8(Card.java PlayCardsMain.java)
Create a class Card, representing a playing card in an ordinary card deck with 52 cards. A card has a suite (4 different) and a rank (13 different). Write a class Deck initially containing 52 different objects of the class Card. The class Deck should contain methods for shuffling the deck, deal a card and telling how many cards are still in the deck. Note that it should only be possible to shuffle a deck if it contains 52 cards. (Information at Wikipedia about card decks and card games.)
Also write a program PlayCardsMain, creating a card deck and dealing some cards, telling the number of remaining cards and which cards that have been dealt.
Hint: Use enumeration types.
- Exercise 9 (VG-exercise)(Deck.java Play123Main.java)
In this exercise you should use the Deck class from the previous exercise. In the patience (single player card game) 1-2-3 you take one card at a time from the deck at the same time as you are counting 1,2,3,1,2,3,1,2,3 etc. You lose the game as soon as you get an Ace when counting "one", a 2 card when counting "two", or a 3 card when counting "three". The chances to win, to make it through the whole deck without losing, are quite small. But how small?
Write a program Play123Main that plays the 1-2-3 game 10000 times and then computes the probability (%) that you win the game. The program should use a method play123 that plays the game once and reports true if you win (or false if you lose) that particular game.
All exercises should be handed in and we are only interested in your .java files. (Notice that the VG exercises 2.4, and 9 are not mandatory.) Hence, zip the directory named YourLnuUserName_assign3 (inside directory named src) and submit it using the Moodle submission system.
Do not hesitate to ask your teaching assistants at the practical meetings (or Jonas at the lectures) if you have any problems. You can also post a question in the assignment forum in Moodle.
Create a new package with the name YourLnuUserName_assign4 inside the Java project 1DV506 and save all program files for this assignment inside that package.
All exceptions related to exercises 1 and 2 should be handled within the programs. Also, exercises 1 and 2 can be handled by a single class respectively. Hence, there is no need for any additional classes apart from the one containing the main method. However, feel free to divide your programs into a number of methods.
- Exercise 1(Histogram.java)
Create a program Histogram.java, reading any number of integers from a file and then printing a histogram bar-chart for all integers between 1 and 100. Note: not all integers in the file are neccesarily in the interval [1-100]. An example of an execution:
Reading integers from file: C:\Temp\integers.dat Number of integers in the interval [1,100]: 46 Others: 16 Histogram 1 - 10 | ****** 11 - 20 | **** 21 - 30 | ** 31 - 40 | *** 41 - 50 | ******* 51 - 60 | **** 61 - 70 | *** 71 - 80 | ********* 81 - 90 | ***** 91 - 100 | ***
Note 1: You will have to create your own data file. We expect it to be an ordinary text file with one integer on each row. Note 2: The absolute path to the data file should be provided as an argument to the main method.
- Exercise 2(CountChar.java)
Write a program CountChar.java, counting characters of different types in a text read from a file. Give the number of characters of the following types:
- Upper case letters
- Lower case letters
- "Whitespace" (i.e. space, tab, return)
- Other characters The path to the text file to read can be coded into the program. (We will test with some other file.) An example of a text file is HistoryOfProgramming. It is a part of an article from Wikipedia about the history of programming. However, we strongly recommend you to start testing your program with a smaller file.
An execution with the file HistoryOfProgramming as input should give the following result:
Number of upper case letters: 86 Number of lower case letters: 3715 Number of "whitespaces": 715 Number of others: 152
If your result does not agree completely with the example above, you have to add a written explanation why you think this happens, to your submission.
- Exercise 3(stack)
Start by creating a new sub package named stack inside your package YourLnuUserName_assign4 and save all .java files related to this exercise inside this package. A stack is a LiFo (Last-in, first-out) data structure with three basic operations: push, pop and peek. push is putting an element on the top of the stack, pop removes (and returns) the top element, and peek returns (without removing) the top element. Think of a stack as a pile of plates that can be found in certain restaurants. You can only add and remove the top-most plate. You can not remove any plates in the middle of the pile (without first removing all plates above it). Your task is to implement the following stack interface:
public interface Stack { int size(); // Current stack size boolean isEmpty(); // true if stack is empty void push(Object element); // Add element at top of stack Object pop(); // Return and remove top element, // exception if stack is empty Object peek(); // Return (without removing) top element, // exception if stack is empty. Iterator<Object> iterator(); // Element iterator }
The iterator traverses all elements currently in the stack, starting with the top element. Illegal operations on an empty stack (e.g., pop() and peek()) should generate an exception. You should also present a test program StackMain.java that demonstrates how each method can be used. Notice: You are not allowed to use any of the data structures in the Java library. However, you can use arrays.
- Exercise 4(sort_cities)
Start by creating a new subpackage named sort_cities inside your package YourLnuUserName_assign4 and save all .java files related to this exercise inside this package. Implement a program SortCities that reads an arbitrary number of city names and their zip codes from a text file. You can assume one city in each line and that each city name (String) and zip code (integer) is separated by a semi-colon(;). Create a class City that represents a city and create a city object for each city you read from the file. The City class should also implement the interface Comparable. Once you have read (and constructed) one City object for each line in the file you should print the cities in a sorted order based on their zip codes. An execution might look like this:
Reading cities from file: C:\Temp\cities.dat Number of cities found: 7 23642 Höllviken 35243 Växjö 51000 Jönköping 72211 Västerås 75242 Uppsala 90325 Umeå 96133 Boden
In the following exercises you should create a number of classes to solve a problem.
- Exercise 5 (The Drunken Walker)(DrunkenWalk.java RandomWalk.java)
Create a class RandomWalk.java, simulating a random walk. A random walk is basically a sequence of steps in some enclosed plane, where the direction of each step is random. The walk terminates when a maximal number of steps have been taken or when a step goes outside the given boundary of the plane. For this task, assume a plane given by a grid, with the point (0, 0) at the center. The size of the plane is given by an integer; if the given integer is k, then the values of the x and y coordinates can vary from -k to k. Each step will be one unit up, one unit down, one unit to the right or one unit to the left (no diagonal movements).
The class RandomWalk will have the following instance data :
- X coordinate of the current position
- Y coordinate of the current position
- The maximum number of steps in a walk
- The number of steps taken so far in the walk
- The size of the plane (according to the description above)
- RandomWalk(int max, int size): the maximum number of steps is max and size is the size of the plane. The start position is set to (0, 0).
- String toString(): returns a string containing the number of steps taken so far and the current position.
- void takeStep(): simulates taking a single step. Generate a random number, taking on four different values, and let them correspond to a movement up, down, to the right and to the left, respectively. The method should also increase the number of steps taken.
- boolean moreSteps(): returns true if the number of steps taken is less than the maximal number of steps, otherwise false is returned.
- boolean inBounds(): returns true if the current position is inside the boundary of the plane, otherwise false is returned.
- void walk(): simulates a random walk, i.e. steps are taken until the maximum number of steps has been taken or until a step goes outside the boundary of the plane.
Create another class DrunkenWalk, simulating walks of drunken people on a platform in a lake. The program should read the boundary, the maximum number of steps, and the number of drunks to simulate. One drunk at a time should be put on the platform and perform its walk. Your program should count how many drunks fall into the water. Test your program for some different values of size and number of steps. Example of an execution:
Enter the size: 10 Enter the number of steps: 50 Enter the number of walks: 150 Out of 150 drunk people, 14 (9.34%) fell into the water.
- Exercise 6 (VG Exercise)(newsagency )
The following exercise description is rather vague, more of a sketchy scenario than a concrete problem specification. Your task is to create the necessary classes to simulate this scenario. All classes should be properly documented and encapsulated. Start by creating a new sub package named newsagency inside your package YourLnuUserName_assign4 and save all .java files related to this exercise inside this package. Newspapers exchange news by using news agencies (such as Reuters and ITAR- TASS). A newspaper registers at a news agency and sends all its news to the agency. The news agency collects the news and broadcasts it to all registered newspapers, except from to the one who delivered it. Create the classes needed to simulate this scenario. Also create a main class to show a simulation where a couple of newpapers generate news and receive news from others.
All exercises should be handed in and we are only interested in your .java files. (Notice that the VG exercise 6 is not mandatory.) Hence, zip the directory named YourLnuUserName_assign4 (inside directory named src) and submit it using the Moodle submission system.