In: Computer Science
public class Runner{
public static void main(String[] args){
MylinkedList ll = new MylinkedList(10.1);
ll.append(15.6);
ll.append(10.5);
ll.append(8.11);
ll.print();
ll.initiateIterator();
Object o = null;
while (
(o=ll.nextObject())!=null){
System.out.println((Double)(o)+100.1);
}
Your solution here
// Iterate, find, and report the largest number
MylinkedList lb = new
MylinkedList();
lb.append( new Rectangle(10.1,
20.2) );
lb.append( new Rectangle(5.3, 15.4)
);
lb.append( new Rectangle(2.3, 4.4)
);
lb.append( new Rectangle(50.3,
20.4) );
lb.print();
Your solution here
//iterate, find, and report the
largest Rectangle
//Rectangle from lb
}
}
-------------------------------------------------------------------------------------------------------
public class Node{
Object data;
Node next;
Node(){}
Node(Object obj){
data = obj;
}
@Override
public String toString(){
return data.toString();
}
}
-----------------------------------------------------------------------------------------------------------------
public class MylinkedList{
Node head;
Node iterator;
MylinkedList(){}
MylinkedList(Object obj){
head = new Node(obj);
}
void initiateIterator(){
iterator = head;
}
void nextOject(){
if (iterator==null)
return
null;
Object returnee =
iterator.data;
iterator = iterator.next;
return returnee;
}
void append(Object onj){
if (head==null)
head = new
Node(obj);
return;
}
Node temp = head;
while (temp.next!=null){
temp=temp.next;
}
temp.next=new Node(obj);
}
void print(){
Node temp =
head;
while(temp! =
null){
System.out.println(temp);
temp=temp.next;
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
public class Rectangle{
double length, width;
Rectangle(){}
Rectangle(double l, double w){
length=l;
width=w;
}
@Override
public String toString(){
return " Length="+length+"
Width="+width;
}
double getArea(){
return length*width;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
The following java files are given. In the first java file which is Runner.java, in the space given above write a code to iterate, find and report the largest number. For the second space given above write a code to iterate and find the largest area for the rectangle.
public class test{
public static void main(String[] args){
MylinkedList ll = new MylinkedList(10.1);
ll.append(15.6);
ll.append(10.5);
ll.append(8.11);
ll.print();
ll.initiateIterator();
Double largest = null;
Object o = null;
// Your solution here
// Iterate, find, and report the largest number
// iterating though the list
while ( (o=ll.nextObject())!=null){
// checking for the largest number
if(largest == null|| (Double)(o) > largest ){
largest = (Double)(o);
}
}
System.out.println("largest Number: "+ largest);
MylinkedList lb = new MylinkedList();
lb.append( new Rectangle(10.1, 20.2) );
lb.append( new Rectangle(5.3, 15.4) );
lb.append( new Rectangle(2.3, 4.4) );
lb.append( new Rectangle(50.3, 20.4) );
lb.print();
// Your solution here
//iterate, find, and report the largest Rectangle
//Rectangle from lb
lb.initiateIterator();
Rectangle largestRectangle = null;
o = null;
// iterating the list
while ( (o=lb.nextObject())!=null){
// checking for the largest area
if(largestRectangle == null|| ((Rectangle)(o)).getArea() > largestRectangle.getArea() ){
largestRectangle = (Rectangle)(o);
}
}
System.out.println("Largest Rectangle: "+ largestRectangle);
}
}
class Node{
Object data;
Node next;
Node(){}
Node(Object obj){
data = obj;
}
@Override
public String toString(){
return data.toString();
}
}
class MylinkedList{
Node head;
Node iterator;
MylinkedList(){}
MylinkedList(Object obj){
head = new Node(obj);
}
void initiateIterator(){
iterator = head;
}
Object nextObject(){
if (iterator==null)
return null;
Object returnee = iterator.data;
iterator = iterator.next;
return returnee;
}
void append(Object obj){
if (head==null){
head = new Node(obj);
return;
}
Node temp = head;
while (temp.next!=null){
temp=temp.next;
}
temp.next=new Node(obj);
}
void print(){
Node temp = head;
while(temp != null){
System.out.println(temp);
temp=temp.next;
}
}
}
class Rectangle{
double length, width;
Rectangle(){}
Rectangle(double l, double w){
length=l;
width=w;
}
@Override
public String toString(){
return " Length="+length+" Width="+width;
}
double getArea(){
return length*width;
}
}