Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I need help with Java Program!!!!!!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Christ1m




PostPosted: Thu Mar 26, 2009 11:07 pm   Post subject: I need help with Java Program!!!!!!

First write the method

public static lp append(lp head1, lp head2)

It takes two lists and puts them together -- first the elements of head1, then the elements of head2. If head1 is empty, the answer is head2. Otherwise, use a recursive call to append head1.rest to head2. Now use "new lp" to add the first element of head1 to the result of the recursive call. That is your answer. Now write

public static lp reverse(lp head)

If the input is empty, return null. Otherwise, use a recursive call to reverse the list head.rest, then use the append function to add head.first to the end of the reversed list. The second argument of append is a list -- not a number. But you can make a list of length one, containing only the number head.first. Then you can use append to add this list to the end of another list. The user should see:

input positive integers: 5 3 8 2 -1
reverse: 2 8 3 5


I need help with the method public static lp reverse(lp head)....Im kind of lost and I dont know what to do.
Also Im having problems with the main method....

Java:

import java.util.*;
import java.io.*;
public class lp
{
public int first;
public lp rest;

public lp(int first, lp rest)
{
this.rest = rest;
this.first = first;
}

public static lp append(lp head1, lp head2)
{
  if(head1==null){
  return head2;
  }
else{
   return new lp(head1.first,append(head1.rest,head2));
}
}
public static lp reverse(lp head)
{
  if(head==null)
  {
    return null;
  }
else
  {
   return new lp(head.first,reverse(head.rest))
   
}// I dont know what to do here....
}
public static String toString(lp head) {
      String str = Integer.toString(head.first);
      if (head.rest != null) str += " " + toString(head.rest);
      return str;
}
 public static void main(String[] args)
  {
 lp list1;
  list1 = new lp(0, new lp(1, new lp(2, new lp(3, new lp(4, new lp(5, new lp(6, new lp(7, new lp(8, new lp(9, null))))))))));

  Scanner scan = new Scanner (System.in);
 
  System.out.println("input positive intergers: ");
  String s = scan.nextLine();
 
 String str = ( "result: " + lp.toString(reverse(list1)));
   System.out.println(str);
}
}
Sponsor
Sponsor
Sponsor
sponsor
Christ1m




PostPosted: Fri Mar 27, 2009 12:20 pm   Post subject: Re: I need help with Java Program!!!!!!

Here is my code.. Im having problems with the main method. How do I print out the result of the recursive functions with the main method.?
Right Now im trying to make the main method ask for inputs to create a list in reverse order .When a negative integer is put, then it stops the loop and prints out the result.
For example
input integer: 1
input integer: 2
input integer: 3
input integer: -1
result: 3 2 1

Java:

import java.util.*;
import java.io.*;
public class lp
{
public int first;
public lp rest;

public lp(int first, lp rest)
{
this.rest = rest;
this.first = first;
}

public static lp append(lp head1, lp head2)
{
if(head1==null){
return head2;
}
else{
return new lp(head1.first,append(head1.rest,head2))...
}
}

public static lp reverse(lp head)
{
if(head==null)
{
return null;
}
if(head.rest == null)
{
return new lp(head.first,reverse(head.rest));

}
else
{
return append(head.rest,reverse(head.rest));
}
}


public static void main(String[] args)
{
// initialize values
int index;
int[] A = new int[1000];

// Initializes the Scanner
Scanner sc = new Scanner(System.in);

// Print out the value
System.out.print("input positive integers:");
int num = sc.nextInt();

// Creates additional scanner acoording to the for loop
for (index =0; num >= 0 && index<1000; index++)
{
A[index] = num;
System.out.print("input integer:");
num = sc.nextInt();
}
// Prints out the sum when user inputs a negative number.
//System.out.println("reverse" + lp.append(head.first,reverse(head.rest))...


}// end of main
}// end of class

DemonWasp




PostPosted: Fri Mar 27, 2009 12:50 pm   Post subject: RE:I need help with Java Program!!!!!!

Assuming you've already got reverse() working and returning the reversed linked list, this should be pretty simple.

1. Start with the head of the list as the "current" node.
2. Output the value of the current node.
3. Advance the current node to the next node.
4. If current is null, stop.
5. Go to step 2.
Christ1m




PostPosted: Sat Mar 28, 2009 1:53 am   Post subject: Re: I need help with Java Program!!!!!!

I dont think my reverse method is right. I need help on it..
So far I created a main method just to test my recursive functions...
All it prints out is the list in the given order. I want it to print it in reverse...
I need help on the append and reverse method...

Java:

import java.util.*;
public class lp
{
public int first;
public lp rest;

public lp(int first, lp rest)
{
this.rest = rest;
this.first = first;
}

public static String toString(lp head) {
      String str = Integer.toString(head.first);
      if (head.rest != null) str += " " + toString(head.rest);
      return str;
}
public static lp append(lp head1, lp head2)
{
  if(head1==null){
  return head2;
  }
else{
   return new lp(head1.first,append(head1.rest,head2));
}
}

public static lp reverse(lp head)
{
  if(head==null)
  {
    return null;
  }
   else
    {
     return new lp(head.first,reverse(head.rest))
    }
}


 public static void main(String[] args)
  {
  lp list1;
    list1 = new lp(0, new lp(1, new lp(2, new lp(3, new lp(4, new lp(5, new lp(6, new lp(7, new lp(8, new lp(9, null))))))))));
    String str = ("reverse: " + lp.toString(reverse(list1)));
   System.out.println(str);
       
 
}// end of main
}// end of class
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: