Results 1 to 3 of 3
  1. #1
    rreimche is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Complete newbie to Java, can't do my homework

    As for the text of my homework (I study, besides other things, software development. We've started with functional programming and haskell and now moved to OOP and Java), I must implement a class and some functions. Seems for me that I have did everything right (or near it), but the compiler says I'm wrong. Here's the code:

    Java Code:
    class List { 
    	double[] content;
    }
    
    List create (){ return new List; }
    
    void append (List l, double elem){ l[l.length+1] = elem; }
    
    int size ( List l ){ return l.length; }
    
    double get ( List l, int index ){ return l[index]; }
    
    void put (List l, int index){
    	l[index] = elem;
    }
    
    void remove ( List l, int index ){ 
    	for ( int i = index; i < l.length-1; i++ )
    		l[i] = l[i+1];
    	l[l.length-1] = null;
    }
    
    boolean contains ( List l, double elem){
    	for ( int i = 0; i<l.length-1; i++ )
    	{	if ( l[i] == elem ) return True; }
    	return False;
    }
    
    public class ListsManipulation {
    	public static void main( String[] args)
    	{}	
    }
    As an error, compiler gives me "class, interface, or enum expected" many times.

    That's the complete listing:

    Roman-Reimche-2:Uebungen rreimche$ javac u9.java
    u9.java:6: class, interface, or enum expected
    List create (){ return new List; }
    ^
    u9.java:6: class, interface, or enum expected
    List create (){ return new List; }
    ^
    u9.java:8: class, interface, or enum expected
    void append (List l, double elem){ l[l.length+1] = elem; }
    ^
    u9.java:10: class, interface, or enum expected
    int size ( List l ){ return l.length; }
    ^
    u9.java:12: class, interface, or enum expected
    double get ( List l, int index ){ return l[index]; }
    ^
    u9.java:16: class, interface, or enum expected
    }
    ^
    u9.java:19: class, interface, or enum expected
    for ( int i = index; i < l.length-1; i++ )
    ^
    u9.java:19: class, interface, or enum expected
    for ( int i = index; i < l.length-1; i++ )
    ^
    u9.java:21: class, interface, or enum expected
    l[l.length-1] = null;
    ^
    u9.java:22: class, interface, or enum expected
    }
    ^
    u9.java:25: class, interface, or enum expected
    for ( int i = 0; i<l.length-1; i++ )
    ^
    u9.java:25: class, interface, or enum expected
    for ( int i = 0; i<l.length-1; i++ )
    ^
    u9.java:26: class, interface, or enum expected
    { if ( l[i] == elem ) return True; }
    ^
    u9.java:28: class, interface, or enum expected
    }
    ^
    14 errors
    I don't know what the compiler likes not...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Complete newbie to Java, can't do my homework

    Please go through the Forum Rules -- particularly the third paragraph.

    Also learn and respect the Java coding conventions in respect of line breaks and indents: Code Conventions for the Java Programming Language: Contents

    You need to start at the beginning. Methods have to be inside a class.
    Lesson: The "Hello World!" Application (The Java™ Tutorials > Getting Started)
    Trail: Learning the Java Language (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Complete newbie to Java, can't do my homework

    The compiler is right 99.9% of the time, and it is right this time. Check all of your opening and closing curly braces, especially the braces that enclose the whole class. Notice anything off?

Similar Threads

  1. Complete newbie
    By diegoigaete in forum New To Java
    Replies: 6
    Last Post: 04-20-2012, 01:18 PM
  2. Steps to create web app for complete newbie
    By motorola in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-16-2011, 12:56 PM
  3. can anyone help me,a complete newbie?
    By keye906 in forum New To Java
    Replies: 13
    Last Post: 06-02-2010, 02:56 PM
  4. Complete Java Removal
    By jpChris in forum New To Java
    Replies: 14
    Last Post: 05-25-2010, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •