| ||||||||||
Tracing:
Given the following code:
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>public class Superhero {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public String name;
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public int strength;
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public Superhero() {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> name = “Batman”;
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> strength = 10;
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
}
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public void introduce() {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> System.out.printspan
class=GramE>(“Hello. I am span
class=GramE>“ + name + “. And
“);
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> if(strength < 15) {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> System.out.printlnspan
class=GramE>(“I am weak.”);
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> } else {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> System.out.printlnspan
class=GramE>(“I am strong.”);
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> }
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>}span style='font-family:
"Courier New";mso-fareast-font-family:"Arial Unicode MS"'>
span style='font-family:"Courier New";mso-fareast-font-family:
"Arial Unicode MS"'>
span style='font-family:"Courier New";mso-fareast-font-family:
"Arial Unicode MS"'>
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>public classspan
style='mso-spacerun:yes'> NinjaTurtle
extends Superhero {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public NinjaTurtle(int
i) {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> super();
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> int[] turtles =
[“Leonardo”, “Donatello”, “Raphael”, “Michaelangelo”];
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> name = turtles[i];
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> strength = 25;
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
}
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
public void introduce() {
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>span
style='mso-spacerun:yes'> System.out.println(“I’m
“ + name);
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>
}
span style='font-size:11.0pt;font-family:"Courier New";
mso-fareast-font-family:"Arial Unicode MS"'>}
span style='font-family:"Courier New";mso-fareast-font-family:
"Arial Unicode MS"'>
What will the output of the following statements be?
>> Superhero
batman = new Superhero();
>> Superhero span
class=SpellE>leo = new NinjaTurtlespan
class=GramE>(0);
>> span
class=SpellE>leo.introduce();
>>
((Superhero) leo).introduce();
>> span
class=SpellE>batman.introduce();
GUI:
You are given the following class:
span style='font-size:11.0pt;
font-family:"Courier New"'>importspan style='font-size:11.0pt;
font-family:"Courier New"'> java.awt.;
span style='font-size:11.0pt;
font-family:"Courier New"'>importspan style='font-size:11.0pt;
font-family:"Courier New"'> javax.swing.;
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'public
class span class=SpellEExampleGUI/span extends span class=SpellEJPanel/span
{o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic span class=SpellEimGUI/span() {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJPanel/span span
class=SpellEbuttonPanel/span = new span class=SpellEJPanel/span();o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJPanel/span span
class=SpellEcontentPanel/span = new span class=SpellEJPanel/span();o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJLabel/span title =
new span class=SpellEJTextField/span(“1316 IM”);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJTextField/span span
class=SpellEreceivedText/span = new span class=SpellEJTextField/span(“Welcome
to 1316 IM”, 25);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJTextField/span span
class=SpellEtoSendText/span = new span class=SpellEJTextField/span(“click
to type”, 5);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJButton/span send =
new span class=SpellEJButton/span(“Send”);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJButton/span warn =
new span class=SpellEJButton/span(“Warn”);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJButton/span chat =
new span class=SpellEJButton/span(“Chat”);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEsetLayout/span(new span
class=SpellEBorderLayout/span());o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEbuttonPanel.add/span(warn);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEbuttonPanel.add/span(chat);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEbuttonPanel.add/span(send);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEcontentPanel.add/span(title,
span class=SpellEBorderLayout.NORTH/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEcontentPanel.add/span(span
class=SpellEreceivedText/span, span class=SpellEBorderLayout.CENTER/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEcontentPanel.add/span(span
class=SpellEtoSendText/span, span class=SpellEBorderLayout.SOUTH/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanadd(span class=SpellEcontentPanel/span,
span class=SpellEBorderLayout.CENTER/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanadd(span class=SpellEbuttonPanel/span,
span class=SpellEBorderLayout.SOUTH/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic static void main(String[] span
class=SpellEargs/span) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEJFrame/span frame =
new span class=SpellEJFrame/span(“IM”);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEframe.setDefaultCloseOperation/span(span
class=SpellEJFrame.EXIT_ON_CLOSE/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spano:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEExampleGUI/span span
class=SpellEex_gui/span = new span class=SpellEExampleGUI/span();o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEframe.getContentPane/span().add(span
class=SpellEex_gui/span);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEframe.pack/span();o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEframe.setVisible/span(true);o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'}o:p/o:p/span/p
p class=MsoNormalo:p /o:p/p
/td
/tr
/table
p class=MsoNormalo:p /o:p/p
p class=MsoNormalspan style='mso-spacerun:yes' /spanspan
style='mso-spacerun:yes' /span/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalb style='mso-bidi-font-weight:normal'Draw the resulting
GUIo:p/o:p/b/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormal!–[if gte vml 1]v:shape id="_x0000_i1026" type="#_x0000_t75"
style='width:402pt;height:347.25pt'
v:imagedata src="FinalExamReview_files/image003.png" o:title="guipicture"/
/v:shape![endif]–![if !vml]img width=536 height=463
src="FinalExamReview_files/image004.jpg" v:shapes="_x0000_i1026"![endif]/p
p class=MsoNormalb style='mso-bidi-font-weight:normal'Picture Methods:o:p/o:p/b/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalThe following is the span class=SpellEspan class=GramEspan
style='font-family:"Courier New"'clearBlue/span/span/spanspan
class=GramEspan style='font-family:"Courier New"'(/span/spanspan
style='font-family:"Courier New"')/span method of the class span
style='font-family:"Courier New"'Picture/span./p
table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0
style='margin-left:5.4pt;border-collapse:collapse;border:none;mso-border-top-alt:
solid windowtext .5pt;mso-padding-alt:0pt 5.4pt 0pt 5.4pt'
tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes;
height:5.0pt'
td width=576 valign=top style='width:432.0pt;border:none;border-top:solid windowtext 1.0pt;
mso-border-top-alt:solid windowtext .5pt;padding:0pt 5.4pt 0pt 5.4pt;
height:5.0pt'
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'/Error: this should not happen
p class=MsoNormal style='margin-left:7.5pt'span style='font-size:11.0pt;
font-family:"Courier New"' Method to clear the blue from the picture (set
span
style='mso-spacerun:yes'> the blue to 0 for all pixels
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span/
public
void clearBlue()
{
span
style='mso-spacerun:yes'> Pixel[] pixels = this.getPixels();
span
style='mso-spacerun:yes'> Pixel pixel =
null;
span
style='mso-spacerun:yes'> int index = 0;
span
style='mso-spacerun:yes'> while(index < pixels.length)
span
style='mso-spacerun:yes'> {
span
style='mso-spacerun:yes'> pixel = pixels[index];
span
style='mso-spacerun:yes'> pixel.setBlue(0);
span
style='mso-spacerun:yes'> index++;
span
style='mso-spacerun:yes'> }
}span
style='font-family:"Courier New"'>
Now write a method to draw vertical stripes (leaving a gap
for the original picture between every stripe) down the picture.span
style='mso-spacerun:yes'> Have every-other stripe be red, and the
others be green.
Public void span
class=SpellE>vertical_stripes()
{
}
Extending the span
class=SpellE>span style='font-family:
"Courier New"'>LLNode
class: (from test 2)
Consider the following span
style='font-family:"Courier New"'>LLNode and span
style='font-family:"Courier New"'>DrawableNode classes:
Abstract
public class LLNode {
span
style='mso-spacerun:yes'> /
span
style='mso-spacerun:yes'> public LLNode
next;
span
style='mso-spacerun:yes'> / Constructor for LLNode/span
just sets next to null/
span
style='mso-spacerun:yes'> public LLNode() {
span
style='mso-spacerun:yes'> next = null;
span
style='mso-spacerun:yes'> }
span
style='mso-spacerun:yes'> /Error: this should not happen
span
style='mso-spacerun:yes'> Methods to set and get next elements
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span @param span
class=SpellE>nextOne next element in list
span
style='mso-spacerun:yes'> /
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void span class=SpellEsetNext/span(span
class=SpellELLNode/span span class=SpellEnextOne/span) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEthis.next/span = span
class=SpellEnextOne/span;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic span class=SpellELLNode/span span
class=SpellEgetNext/span() {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanreturn span class=SpellEthis.next/span;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span/Error: this should not happen
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span Method to remove node from list, fixing
links
span
style='mso-spacerun:yes'> appropriately
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span @param node
element to remove from list.
span
style='mso-spacerun:yes'> /
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void remove(span class=SpellELLNode/span
node) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span/Error: this should not happen
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span Insert the input node after this node
span
style='mso-spacerun:yes'> @param/span node
element to insert after thiso:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanError: this should not happen/
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void span class=SpellEinsertAfter/span(span
class=SpellELLNode/span node) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'}
//end of span class=SpellELLNode/span classo:p/o:p/span/p
/td
/tr
/table
p class=MsoNormalspan style='font-size:11.0pt'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt'o:p /o:p/span/p
table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0
style='margin-left:5.4pt;border-collapse:collapse;border:none;mso-border-alt:
solid windowtext .5pt;mso-padding-alt:0pt 5.4pt 0pt 5.4pt;mso-border-insideh:
.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext'
tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes;
height:9.0pt'
td width=576 valign=top style='width:432.0pt;border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;padding:0pt 5.4pt 0pt 5.4pt;height:9.0pt'
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'public
class span class=SpellEDrawableNode/span {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic span class=SpellEDrawableNode/span
next;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic span class=SpellEDrawableNode/span()
{o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spannext = null;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void span class=SpellEsetNext/span(span
class=SpellEDrawableNode/span span class=SpellEnextOne/span) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellEthis.next/span = span
class=SpellEnextOne/span;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic span class=SpellEDrawable/span span
class=SpellEgetNext/span() {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanReturn span class=SpellEthis.next/span;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void remove(span class=SpellEDrawableNode/span
node) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanpublic void span class=SpellEinsertAfter/span(span
class=SpellEDrawableNode/span node) {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span//
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span}o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'}//
end of span class=SpellEDrawableNode/span classo:p/o:p/span/p
/td
/tr
/table
p class=MsoNormalo:p /o:p/p
p class=MsoNormalPlease rewrite the span class=SpellEspan
style='font-family:"Courier New"'DrawableNode/span/span class to have it
extend the span class=SpellEspan style='font-family:"Courier New"'LLNode/span/span
class.span style='mso-spacerun:yes' /spanYou are to retain in the new
version of span class=SpellEspan style='font-family:"Courier New"'DrawableNode/span/span
only the code that is the essence of handling pictures, removing all
functionality that is to be handled by span class=SpellEspan
style='font-family:"Courier New"'LLNode/span/span./p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalb style='mso-bidi-font-weight:normal'Insertion Sort:o:p/o:p/b/p
p class=MsoNormalo:p /o:p/p
p class=MsoNormalBelow is the insertion span class=GramEspan
style='font-family:"Courier New"'sort(/span/spanspan style='font-family:
"Courier New"')/span method from span class=SpellEspan style='font-family:
"Courier New"'EventQueue/span/span:/p
p class=MsoNormalo:p /o:p/p
table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0
style='margin-left:5.4pt;border-collapse:collapse;border:none;mso-border-alt:
solid windowtext .5pt;mso-padding-alt:0pt 5.4pt 0pt 5.4pt;mso-border-insideh:
.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext'
tr style='mso-yfti-irow:0;mso-yfti-firstrow:yes;mso-yfti-lastrow:yes;
height:18.0pt'
td width=576 valign=top style='width:432.0pt;border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;padding:0pt 5.4pt 0pt 5.4pt;height:18.0pt'
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'Public
void sort() {o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellESimEvent/span
considered = null;o:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /spanspan class=SpellESimEvent/span span
class=SpellEcompareEvent/span = null; //Just for use in the loopo:p/o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'o:p /o:p/span/p
p class=MsoNormalspan style='font-size:11.0pt;font-family:"Courier New"'span
style='mso-spacerun:yes' /span/
Small index we’re comparing to /
span
style='mso-spacerun:yes'> int compare;
span
style='mso-spacerun:yes'> for(int position =
1;position < elements.size();position++){
span
style='mso-spacerun:yes'> considered = (SimEvent)
elements.get(position);
span
style='mso-spacerun:yes'>
span
style='mso-spacerun:yes'> compare = position;
span
style='mso-spacerun:yes'> compareEvent =
(SimEvent) elements.get(compare-1);
span
style='mso-spacerun:yes'> while(compareEvent.getTime()
> considered.getTime()) {
span
style='mso-spacerun:yes'> elements.set(compare,
elements.get(compare-1));
span
style='mso-spacerun:yes'> compare = compare – 1;
span
style='mso-spacerun:yes'> // If we get to the end of the array,
stop
span
style='mso-spacerun:yes'> if(compare <= 0) {break;}
span
style='mso-spacerun:yes'> // else we get ready for the next time
through the loop
span
style='mso-spacerun:yes'> else { compareEvent
= (SimEvent)elements.get(compare-1);}
span
style='mso-spacerun:yes'> }
span
style='mso-spacerun:yes'> elements.set(compare,
considered);
span
style='mso-spacerun:yes'> }// for all positions 1 to the end
}//
end sort()
A. Why do we need two loops for this sorting algorithm?span
style='mso-spacerun:yes'> Explain the purpose of each.
B. How would we go about reversing the sort (lets say we
wanted the most time consuming element to happen first)?
Writing Linked List
methods:
Below is the span style='font-family:
"Courier New"'>remove()
method taken from the class LLNode
public void
remove(LLNode node) {
span
style='mso-spacerun:yes'> if(node == this) {
span
style='mso-spacerun:yes'> System.out.println(“I
can’t remove the first node”);
span
style='mso-spacerun:yes'> return;
span
style='mso-spacerun:yes'> }
span
style='mso-spacerun:yes'>
span
style='mso-spacerun:yes'> LLNode current =
this;
span
style='mso-spacerun:yes'> while(current.getNext()
!= null)
span
style='mso-spacerun:yes'> {
span
style='mso-spacerun:yes'> if(current.getNext()
== node) {
span
style='mso-spacerun:yes'> current.setNext(span
class=SpellE>node.getNext());
span
style='mso-spacerun:yes'> node.setNext(null);
span
style='mso-spacerun:yes'> return (null);
span
style='mso-spacerun:yes'> }
span
style='mso-spacerun:yes'> current = current.getNext();
span
style='mso-spacerun:yes'> }
}
Write a method that will take in a number and delete that
node from the list (remember to check if the number is longer than your list).
For example, if our list looks like A -> B -> C ->
D -> null and we call the method span style='font-family:
"Courier New"'>A.remove(3),
our list should look like A -> B -> D -> null. span
style='mso-spacerun:yes'> (we start our
indexing at 1).
publicspan
style='font-family:"Courier New"'> void remove(int
index) {
}
Trees:
Consider the code:
public
class MySoundTree {
span
style='mso-spacerun:yes'> ScaleBranch span
class=SpellE>scaledBranch; //
Needed between methods
span
style='mso-spacerun:yes'> SoundBranch root;
span
style='mso-spacerun:yes'> public void setup() {
span
style='mso-spacerun:yes'> FileChooser.setMediaPath(“C:/cs1316/MediaSources/”);
span
style='mso-spacerun:yes'> Sound clink =
span
style='mso-spacerun:yes'> new Sound(FileChooser.getMediaPath(“clink-span
class=SpellE>tenth.wav”));
span
style='mso-spacerun:yes'> Sound clave =
span
style='mso-spacerun:yes'> new Sound(FileChooser.getMediaPath(“clave-span
class=SpellE>tenth.wav”));
span
style='mso-spacerun:yes'> Sound clap =
span
style='mso-spacerun:yes'> new Sound(FileChooser.getMediaPath(“clap-span
class=SpellE>q.wav”));
span
style='mso-spacerun:yes'> Sound chirp =
span
style='mso-spacerun:yes'> new Sound(FileChooser.getMediaPath(“chirp-2.wav”));
span
style='mso-spacerun:yes'> root = new SoundBranch();
span
style='mso-spacerun:yes'> SoundNode span
class=SpellE>sn;
span
style='mso-spacerun:yes'> SoundBranch
branch1 = new SoundBranch();
span
style='mso-spacerun:yes'> sn = new span
class=SpellE>SoundNode(clap.append(clink));
span
style='mso-spacerun:yes'> Branch1.addChild(sn);
span
style='mso-spacerun:yes'> sn = new span
class=SpellE>SoundNode(clave);
span
style='mso-spacerun:yes'> Branch1.addChile(sn);
span
style='mso-spacerun:yes'> Root.addChild(branch1);
span
style='mso-spacerun:yes'> ScaleBranch span
class=SpellE>scaledBranch = new ScaleBranch(1.6);
span
style='mso-spacerun:yes'> sn = new span
class=SpellE>SoundNode(chirp);
span
style='mso-spacerun:yes'> scaledBranch.addChild(span
class=SpellE>sn);
span
style='mso-spacerun:yes'> root.addChild(span
class=SpellE>scaledBranch);
span
style='mso-spacerun:yes'> root.playFromMeOn();
span
style='mso-spacerun:yes'> }
}span
style='font-family:"Courier New"'>
Draw the resulting structure (using boxes and arrows) from
the setUpspan
class=GramE>(span
style='font-family:"Courier New"'>) method.span
style='mso-spacerun:yes'> Be sure to label each node with a variable
name, class name, and sound. Also, don’t
forget to include your null references.