Laura Peterson

Greenhorn
+ Follow
since Mar 14, 2013
Laura likes ...
Eclipse IDE Chrome Windows
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Laura Peterson

Thanks for the new thought process. I think I'm understanding a little better now. Thank you!
6 years ago
My output does not have the same spacing that the sample output does.
6 years ago
Greetings, fellow Java programmers! I have an assignment (yes, a homework one, but I actually DID it, my class TA is awol and I have no one to ask questions) that I have two errors in that I can't seem to correct. I am also including the input file, the sample output file, and the assignment outline (sorry for such a long post- can anyone tell me what file formats are accepted for attachments? It seems like everything I try doesn't work). My problems are with the inOrderPrint/printInorder (lines 69 & 78, somehow the output isn't spacing correctly) and the searchTree (line 147, when I go through the debugger everything looks fine, but it doesn't terminate properly) methods. Any help and advice as far as fixing those bugs for the correct output would be welcomed. Thanks so much for your time!

Here is my code:


Here is the interface code:


The Node class code:


And the test class code:






And the description:


This assignment will give you practice with trees. You are to write a class MorseCoder that allows you to encode and decode Morse code.
The Morse code (see Table 6.10) is a common code that is used to encode messages consisting of letters and digits. Each letter consists of a series of dots and dashes; for example, the code for the letter a is •– and the code for the letter b is –•••. Store each letter of the alphabet in a node of a binary tree of level 5. The root node is at level 1 and stores no letter. The left node at level 2 stores the letter e (code is •), and the right node stores the letter t (code is –). The 4 nodes at level 3 store the letters with codes (••, •–, –•, ––). To build the tree (see Figure below), read a file in which each line consists of a letter followed by its code. The letters should be ordered by tree level. To find the position for a letter in the tree, scan the code and branch left for a dot and branch right for a dash. Encode a message by replacing each letter by its code symbol. Then decode the message using the Morse code tree.
Your class must implement the MorseCodeInterface provided and must contain the methods specified. You may need to provide a constructor to build the tree from the input file, encodings.txt. You may create private methods to implement the recursion for the different methods.
You are allowed to declare whatever data fields you want for your class, but you should try to keep these to a minimum. You can get by with just one data field (possibly root) and you shouldn’t need much more. If you have unnecessary data fields (data fields that could be turned into local variables), you will lose style points.
In terms of correctness, your class must provide all of the functionality described above. In terms of style, we will be grading on your use of comments, good variable names, consistent indentation and good coding style to implement these operations.
You should name your file MorseCoder.java and you should turn it in electronically from the “assignments” link on the class web page. You will need to have MorseCodeInterface.java, MorseNode.java, MorseMain.java and encodings.txt (In the same level as src) all in the same directory as your MorseCoder.java in order to run MorseMain. A sample output file, output.txt file is also attached.
You may work on this assignment in pairs.

6 years ago
I've changed the computeTo method to this:



Any reason why I am getting the number 3 twice?
6 years ago
Oops! Sorry about that! I've edited the original post to contain the correct LinkedQueue class. Thanks for the advice about the queue. Are you saying that I should actually create an iterator? Thank you!
6 years ago
Greetings, Java programmers! I am seeking your wisdom once again for an error I can't seem to quite put my finger on. I am working on a problem that computes primes. Here is the problem:

You are going to implement a class that computes all the primes up to some integer n. The technique you are to use was developed by a Greek named Eratosthenes who lived in the third century BC. The technique is known as the Sieve of Eratosthenes. The algorithm is described by the following pseudocode:

create a queue and fill it with the consecutive integers 2 through n inclusive.
create an empty queue to store primes.
do {
obtain the next prime p by removing the first value in the queue of numbers.
put p into the queue of primes.
go through the queue of numbers, eliminating numbers divisible by p.
} while (p < sqrt(n))
all remaining values in numbers queue are prime, so transfer them to primes queue

You are to use the Queue interface provided. When you want to construct a Queue object, you should make it of type LinkedQueue. These classes are included.
You should define a class called Sieve with the following public methods:

Sieve() - Constructs a sieve object.

void computeTo(int n) - This is the method that should implement the sieve algorithm. All prime computations must be implemented using this algorithm. The method should compute all primes up to and including n. It should throw an IllegalArgumentException if n is less than 2.

void reportResults() - This method should report the primes to System.out. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method. It is okay for it to have an extra space at the end of each line.

int getMax() - This is a convenience method that will let the client find out the value of n that was used the last time computeTo was called. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.

int getCount() - This method should return the number of primes that were found on the last call on computeTo. It should throw an IllegalStateException if no legal call has been made yet on the computeTo method.

Your reportResults method should print the maximum n used and should then show a list of the primes, 12 per line with a space after each prime. Notice that there is no guarantee that the number of primes will be a multiple of 12. The calls on reportResults must exactly reproduce the format of the sample log. The final line of output that appears in the log reporting the percentage of primes is generated by the main program, not by the call on reportResults.



Here is my class Sieve. I am having difficulty getting all the primes into the proper queue:


When I input say, 20, I only get 2, 3, and 11 back as primes. Somewhere in the algorithm (lines 40-54) I seem to have gone awry, but I'm not certain where. If anyone can see how or why, I'd greatly appreciate the correction!

Here are the other classes:

SieveMain:


Queue:


and LinkedQueue:


Thank you very, very much!
6 years ago
Thank you very much for your help, encouragement, and replies. I'll work on the one relationship part in my spare time. I'm currently taking a C# class, but my instructor recommended a thorough knowledge of Java, so I'm learning as much as I can myself. Javadoc seems very intimidating, I'm not sure I'm ready for that yet- but I'll try!

Just a note- I used "number" because it was the term used in the instructions. As I stated before, I'm trying to stay as close to them as possible- but I will remember the importance of good names for the future.

And last... yes, I am from Seattle.
7 years ago
Okay... thanks for your patience with me. I'm VERY much a beginner at all of this stuff! I think I finally understand what you're saying- and here is my revised code. It seems to be working the way it should (though "seems" is a flexible word).

The class:


The test class:


Thanks for any additional pointers and tidbits for the future!

7 years ago
Thanks so much for all the help. I've figured out the simulcast method- here's what I have, and it outputs correctly and in the manner the directions specify (I'm storing away the information about private, etc, for future use- right now I'm trying to do it as simply as possible and as close to the directions as I can)-



The compareTo method is still giving me trouble, though. I seem to get the gist of it, but for some reason the AM and FM aren't grouping correctly- the first station always stays in the same place. What can I do to correct this? Here's my compareTo code:



Many thanks again!
7 years ago
Okay, everyone! I have a new practice problem I'm working on- I get the general sense of it, but can't seem to tweak it to meet the requirements! Here's the problem:

Define a class RadioStation that can be used to store information about radio stations. For each station, keep track of its name (a string), its broadcast band (a string) and its station number (a real number). For example, there is a local station called KUOW that is an FM station broadcast on 94.9. The class must have the following public methods:

RadioStation(name, band, number) constructs a radio station with given name, band and station number

getName() returns the name

getBand() returns the band

getNumber() returns the number

toString() returns a String representation of the radio station

simulcast(other) records the fact that this station and the other are simulcasts

The broadcast band can be any arbitrary string. For example, it might be "AM" versus "FM" or might include subdivisions like "FM 1" and "FM 2" or might include other text like "XM" for satellite radio. The toString method should return a String composed of the station name followed by the band followed by the station number. For example, if you were to construct the following station:

RadioStation station = new RadioStation("KKNW", "AM", 1150.0);

then a call on station.toString() should return "KKNW AM 1150.0".

The simulcast method is used to record a relationship between two stations that are broadcasting the same material, as in:

RadioStation station1 = new RadioStation("KUOW", "FM", 94.9);
RadioStation station2 = new RadioStation("KUOW", "AM", 1340);
station1.simulcast(station2);

The call on simulcast indicates a link between the two stations. The link goes in both directions, so it shouldn't matter whether you make the call above or if you instead make the following call:
station2.simulcast(station1);

A given station will have at most one simulcast relationship. When such a relationship exists, it should be included in the result of toString. Given the calls above, the call station1.toString() should return:

KUOW FM 94.9 (simulcast on AM 1340.0)

and the call station2.toString() should return:

KUOW AM 1340.0 (simulcast on FM 94.9)

Notice that the simulcast notation appears in parentheses and that we include just the band and station number. You are to exactly reproduce this format. Your class should implement the Comparable<E> interface. Radio stations should be grouped together by band (e.g., AM stations grouped together and FM stations grouped together). Within a given band, the stations should be sorted by station number (e.g., FM 94.9 less than FM 96.5).

I'm struggling with the simulcast method (mine basically returns a string) and grouping the stations first by band, then by number (rather than just number, as I have). Any help, hints, or comments would be appreciated!

Here is my class:


And here is the test class:
7 years ago
Thanks to everyone- I did manage it. Thanks for all the helpful hints and pointers about general use, too!
7 years ago
So I'm still having trouble with classes. I'm working on a new problem, which goes something like this:

Write a class named GroceryList that represents a person's list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: 4 boxes of cookies).
The GroceryList class should use an array field to store the grocery items, as well as keeping track of its size (number of items in the list so far). Assume that a grocery list will have no more than 10 items. A
GroceryList object should have the following methods:

public GroceryList() - Constructs a new empty grocery list.
public void add(GroceryItemOrder item) - Adds the given item order to this list, if the list is not full (has fewer than 10 items).
public double getTotalCost() - Returns the total sum cost of all grocery item orders in this list.

The GroceryItemOrder class should store an item quantity and price per unit. A GroceryItemOrder object should have the following methods:

public GroceryItemOrder(String name, int quantity, double pricePerUnit) - Constructs an item order to purchase the item with the given name, in the given quantity, which costs the given price per unit.
public double getCost() - Returns the total cost of this item in its given quantity. For example, 4 boxes of cookies that are 2.30 per unit have a cost of 9.20.
public void setQuantity(int quantity) - Sets this grocery item's quantity to be the given value.

I'm pretty sure I have the GroceryItemOrder class done correctly, and I have my test/main method class done... It's the GroceryList that I seem to have trouble working with the array and using it in conjunction with the other class. Here's what I have:

GroceryList Class:


GroceryItemOrder Class:


Main Method/Test Class:


Any assistance would be appreciated, and thank you!

7 years ago
You've both been quite helpful... I'll get to work on your hints right away... thanks so much!

- It works now, thanks again!
7 years ago
I am working on some problems to test my meager understanding of classes in Java. The problem I'm currently working on goes:

Write a class called Time that can be used to store a time in hours, minutes and seconds. It should have the following public methods:
Time() A constructor that takes no parameters and that constructs an object representing 0 time.

Time(int, int, int) A constructor that takes three integers (hours, minutes, seconds) and that constructs a Time object storing that time.

void add(int, int, int) A method which takes a time as three integers (hours, minutes, seconds) and that adds the time to the current time.

double getTime() A method which returns the time as the real-valued number of hours with a decimal (as in 9.53).

String toString() toString which returns a string representation of the time (hh:mm:ss) When converted to a string, the seconds and minutes should always be reported as being in the range of 0 to 59. That means that you may have to "carry" a full hour or minute to the next column.

I think I've figured everything out except the void add method- I've tried numerous ways and can't seem to get anywhere. Here's what I have so far, and any pointers or assistance would be much appreciated!

7 years ago