Download the L08.zip file and extract
the files to your cs125 folder in your home directory.
You will use these files to complete this week's lab exercises.
This lab covers objects. A quick overview is given here.
There are situations that require encrypted text. One of the simplest ways of encoding a text message is to use an alphabet cipher. In an alphabet cipher, every letter in the message is shifted a certain number of letters forward. For example if the shift amount was 5, then all occurrences of the letter 'A' in the original message would become the letter 'F in the encoded message, all occurrences of the letter 'B' would become the letter 'G', and so on. At the end of the alphabet, the shift wraps back to the beginning. For example, the letter 'Y' would shift to the letter 'D' and the letter 'Z' would shift to the letter 'E'.
A more sophisticated version of this cipher uses a key (a word or phrase) to handle the shifting. In this case the first letter of the key indicates the amount of shift for the first letter of the message. The second letter of the key indicates the amount of shift for the second letter of the messages, and so on. Once you have reached the end of the keyword, the next letter in the message shifts according to the first letter of the keyword, and the cycle continues. The amount of the shift depends upon how far the letter is away from the beginning of the alphabet. For example, if the keyword is "CODE", the first letter will be shifted by 2, the second by 14, the third by 3, and the fourth by 4. Notice that if the letter 'A' is part of the keyword, there would be no shifting from the original message letter to the coded letter. Here is an example of an encoding:
| Original | H | E | L | L | O | T | H | E | R | E | |
| Shift | 2 | 14 | 3 | 4 | 2 | 14 | 3 | 4 | 2 | 14 | |
| Coded | J | S | O | P | Q | H | K | I | T | S |
You are to complete a class called Coder which will handle encoding and
decoding a message using this technique. The class diagram for Coder is here
|
|
|
In particular, you must implement the constructor, and the
two methods: encode
and decode.
More details about these methods can be found in the starter file Coder.java. You have
also been provided with another file called CoderTester.java that contains some test
cases for your Coder
class. You may add your own test cases to the main method if you wish.
In order to implement the methods properly, you need to
remember that all characters have an integer code associated with them known as
their Unicode value. In Java, you can add and subtract char values as you would numbers, but
the result is an integer. For example in Java 'a' + 'b' is 195 since this the sum of the Unicode
values of 'a' and
'b'. You can
also add integers to char
values. For example 'a' + 1
is 98, which
also happens to be the Unicode value of 'b'.
While implementing the encode and decode methods, you can
calculate the shifted value of the characters by using the Unicode integer
values. For example, if keyCharacter is the current character in the key
shift = keyCharacter - 'A'
message.charAt(i) + shift
message.charAt(i) - shift
However, you will need to convert the Unicode back to a character once
you have figured out the new character's Unicode value. The Coder class has a private method
called intToChar
that will handle that conversion. You can call this method and pass an integer
parameter, and the method will return the appropriate char value. For example if you call
this method with the parameter value 98, the method will return 'b'.
Submit the completed file Coder.java below.
You have been provided with a class called Computer
which represents computer attributes.
The class has the following methods
Computer |
- String brand - double cpuSpeed - int RAM |
+ Computer(String type, double speed, int memory) + void makeSameSpeed(Computer other) + void doubleSpeed() + boolean sameBrand(Computer other) + String getUpperBrand() + boolean equals(Computer other) + String toString() |
Documentation for each of these methods can be found
in the source code for the Computer class.
toString()Most classes should define a toString() method.
This method returns a String version
of the object using the instance variable(s). It is a
method that can be useful in debugging.
You can call this method in the same way that other methods are
called, but
the toString() method is called automatically whenever
you use an object in a print/println statement.
Java creates a default toString method if you
do not create your own which cryptically prints out where the
object is in memory.
Currently the toString() method in the
Computer class has been commented out.
Question1 class.
Notice what is displayed by System.out.println statements. Why do you think the first and third
outputs are the same and the second output is different?toString method in the Computer class. Run the main method in Question1 again.
Briefly explain why the output looks different now.toString method so that
it prints out the values of instance variables instead.
Submit your new toString() method here.
The main method in the Question2a class creates two computers that are the same, and then
doubles speed of the first computer. Then it displays the
speed of the two machines.
The main method in the Question2b class appears to do the same thing in a
different way. Do you get the same output in each case? Explain why.
The makeSameSpeed(Computer other) method is
supposed to change the speed of other to
match the speed of this.
When you run the main method in the Question3 class, do you
get the expected result? What do you get? Why?
Rewrite the makeSameSpeed method to
work correctly. Submit your new makeSameSpeed method here.
If you request solutions for this lab you will be sent an email that contains your answers as well as the expected answers. The email will be sent out to all interested students once the deadline for completing the exercises has passed for all students.