Consider the following code:
public class Element {
private Element next;
public Element getNext() {return this.next;}
public void setNext(Element next) {this.next = next;}
public int length() {return 0;}
public static void main(String[]args){
Element node1 = new Element();
Element node2 = new Element();
Element node3 = new Element();
Element node4 = new Element();
node1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(node1);
System.out.println(node1.length());
}
}
Rewrite the length() method so that when called it will print out the correct number of elements in the list. Remember that you do not know how many elements will be in the list so you cannot use a for-loop. You also cannot alter the linked list structure in any way.
public int length()
Picture methods
Consider the following code:public void grayscale(){
Pixel pixel = null;
int intensity = 0;
Pixel[] pixels = this.getPixels();
for (int i = 0; i < pixels.length; i++){
pixel = pixels[i];
intensity = (int) ((pixel.getRed() + pixel.getGreen() +
pixel.getBlue()) / 3);
pixel.setColor(new Color(intensity,intensity,intensity));
}
}
Write a Picture class method called public static int countThatColor(Picture picture, Color color, int range) that counts all the colors in an inputted picture that fall within a range of the inputted color. Also write a main method that will test your countThatColor method.
public static int countThatColor(Picture picture, Color color, int range)
From Increase to Reverse
Below is the code from Sound for increasing the volume of a sound.
/**
* Increase the volume of a sound
**/
public void increaseVolume(double factor){
SoundSample [] samples = this.getSamples();
SoundSample current = null;
for (int i=0; i < samples.length; i++) {
current = samples[i];
current.setValue((int) (factor * current.getValue()));
}
}
Write the method to reverse a sound.
Draw the Tree
Below is a method from SoundTreeExample. Draw the data structure that it defines. BE SURE TO LABEL children versus next nodes!
public void setUp2() {
FileChooser.setMediaPath("D:/cs1316/MediaSources/");
Sound clap = new Sound(FileChooser.getMediaPath("clap-q.wav"));
Sound chirp = new Sound(FileChooser.getMediaPath("chirp-2.wav"));
Sound rest = new Sound(FileChooser.getMediaPath("rest-1.wav"));
Sound snap = new Sound(FileChooser.getMediaPath("snap-tenth.wav"));
Sound clink = new Sound(FileChooser.getMediaPath("clink-tenth.wav"));
Sound clave = new Sound(FileChooser.getMediaPath("clave-twentieth.wav"));
Sound gong = new Sound(FileChooser.getMediaPath("gongb-2.wav"));
Sound guzdial = new Sound(FileChooser.getMediaPath("guzdial.wav"));
Sound is = new Sound(FileChooser.getMediaPath("is.wav"));
root = new SoundBranch();
SoundNode sn;
SoundBranch branch1 = new SoundBranch();
sn = new SoundNode(guzdial.append(is).append(snap));
branch1.addChild(sn);
sn = new SoundNode(clink.append(snap).append(clave));
branch1.addChild(sn);
sn = new SoundNode(guzdial.append(is).append(is));
branch1.addChild(sn);
root.addChild(branch1);
scaledBranch = new ScaleBranch(2.0);
sn = new SoundNode(clink.append(clave).append(gong));
scaledBranch.addChild(sn);
sn = new SoundNode(rest.append(chirp).append(clap));
scaledBranch.addChild(sn);
root.addChild(scaledBranch);
scaledBranch = new ScaleBranch(0.5);
sn = new SoundNode(guzdial.append(is).append(gong));
scaledBranch.addChild(sn);
root.addChild(scaledBranch);
SoundBranch branch2 = new SoundBranch();
sn = new SoundNode(clap.append(clap).append(clave));
branch2.addChild(sn);
sn = new SoundNode(snap.append(snap).append(clave));
branch2.addChild(sn);
sn = new SoundNode(snap.append(snap).append(clave));
branch2.addChild(sn);
root.addChild(branch2);
root.playFromMeOn();
}
Inheritance
public abstract class Car {
private double mpg;
public abstract void drive();
public void repair() { System.out.println("Car’s repair"); }
}//end class Car
public class Toyota extends Car {
public void drive() { System.out.println("Toyota’s drive"); }
}//end class Toyota
public class Avalon extends Car {
public void drive() { System.out.println("Avalon's drive"); }
public void zoom() { System.out.println("Avalon's zoom"); }
}//end class Avalon
public class Camry extends Toyota {
public void drive() { System.out.println("Camry's drive"); }
public void washMe() { System.out.println("Camry's washMe");}
}//end class Camry
Given the above classes decide whether the following segments of code will compile or not (be sure to consider both lines):
Car a = new Camry();
a.repair();
Avalon b = new Car();
b.drive();
Car c = new Car();
c.drive();
Car d = new Camry();
d.drive();
Toyota e = new Toyota();
e.repair();
Car f = new Avalon();
f.zoom();
Car g = new Avalon();
((Avalon)g).zoom();
Car h = new Camry();
((Avalon)h).zoom();
Given
Toyota i = new Camry();
i.drive();
What is the output?
Properties of Data Structures
Data structures that we discussed this semester include arrays, matrices, linked lists, circular linked lists, trees, graphs, stacks, queues, and event queues.
One or more data structures match each of the following statements or descriptions. List the data structures for which the statement is true:
A. Can change size dynamically to match the size of the data.
B. Is good for representing hierarchy.
C. Is particularly hard to insert into the middle of.
D. Can take a while to find an element in this data structure.
E. Is good for representing a line of people waiting for a ferris wheel ride.
F. Is good for representing animation states of a character.
G. Is used for representing the samples in a sound.
H. Is used for representing the pixels in a picture.
Finding Max for Two Structures
A. Write a void method called findMax(int[]array) that takes an array of integers as a parameter and it finds and prints both the index and the value of the largest element in the array. Your method should print out a String like the following example:
The largest value is 3000 found at array index position 5.
B. Write a void method called findMax(IntNode head) that takes a linked list of integers as a parameter and it finds and prints both the index and the value of the largest element in the list. Assume that IntNode inherits from LLNode, and each IntNode contains a int instance variable named value, which you should access through the public int accessor getValue(). Your method should print out a String like the following example:
The largest value is 3000 found in the list is at position 5.
Link to this Page