The Artima Developer Community
Sponsored Link

Java Answers Forum
how to write a driver for IntList.java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

how to write a driver for IntList.java Posted: Apr 1, 2006 7:05 PM
Reply to this message Reply
Advertisement
I don't quite know how to write a driver to test out some of the methods, i could only get the average, but for the void methods,moveToFront and countCommonValues, how would you write the driver to test it?

Driver:
public class IntListDriver
{
	public static void main(String[] args)
	{
		IntNode head=null;
		for (int i = args.length - 1 ; i >= 0 ; i--) {
			head = new IntNode(Integer.parseInt(args[i]), head);
		}
 
 
		IntList list = new IntList();
		list.head=head;
 
		double result;
		result=list.getAverage();
		System.out.println(result);
	}
}





Class:
public class IntList
{
	public IntNode head; // DO NOT MAKE THIS FIELD PRIVATE
	public IntList()
	{
		head = null;
	}
 
	public double getAverage()
	{
		double count=0;
		double average=0;
		IntNode pos=head;
		while (pos!=null)
		{
			count++;
			average=average+pos.data;
			pos=pos.next;
		}
		
		average=average/count;
		return average;
		else	
		{
			return null;
		}
		
		//ERROR CONDITIONS!
	}
 
	public void addAfterEvery(int oldvalue, int newvalue)
	{
		IntNode pos=head;
		IntNode prev=null;
		while (pos!=null)
		{
			if (oldvalue==pos.data)
			{	
				prev=pos.next;
				IntNode newNode=new IntNode();
				newNode.data=newvalue;
				pos.next=newNode;
				newNode.next=prev;
				pos=newNode.next;
			}
			else
			{
				pos=pos.next;
			}	
		}
	}
 
	public void moveToFront(int value)
	{
		IntNode pos=head;
 
		if (pos==null)
		{
			return;
		}
		
		IntNode p=null;
 
		while (pos.next!=null)
		{
			if (value==pos.next.data)
			{
				p=pos;
				pos=pos.next;
				if (pos!=null)
				{
					p.next=pos.next;
					pos.next=head;
					pos=p;
				}
			}
			else
			{
				pos=pos.next;
			}
		}
 
			
	}
 
	public void reverse()
	{
		//IntNode
		//pos.next=null;
		//while (pos.next!=null)
		//{
		//	pos.next=pos;
		//	
		//}
		//head=pos;
		
	}
	
	public int countCommonValues(IntList other)
	{
		IntNode pos=head;
		IntNode posother=other.head;
		
		int count=0;
		while (pos!=null)
		{
			if (pos==posother)
			{
			count++;
			pos=pos.next;
			posother=posother.next;
			}
			else
			{
			pos=pos.next;
			posother=posother.next;
			}
		}
		return count;
	}
}

Topic: Free Java Tutorials SITE Previous Topic   Next Topic Topic: Plz help me for java code

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use