Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Stuck with Java method

Name: Anonymous 2007-02-12 5:54

Hello, I am currently working on a Java program and I have come across a bit of a problem. I am using an interface and two classes to make a list of objects. The program currently includes an interface List, a class ConsList which implements List and contains an object "first" and a ConsList "rest", as well as a class EndList implementing List which contains an object "last" and is used to terminate the list. An example of use would be:
List l = new ConsList(foo,new ConsList(foo2, new EndList(end)));

All classes implementing List need to have a shiftRight(object o) method, which puts "o" at the beginning of the list, and removes the last object from the list. I have been trying to write this for a while now, but as I am not exceptional at Java the proper method definition continues to elude me. I would greatly appreciate any help. Thank you in advance.

Name: Anonymous 2007-02-12 6:07 (sage)

fuck off javafag

Name: Anonymous 2007-02-12 6:09

Not appreciated, >>2.

Name: Anonymous 2007-02-12 10:16

Java sucks

Name: Anonymous 2007-02-12 10:33 (sage)

For the ConsList:
  rest.shiftRight(first);
  first = o;
For the EndList:
  last = o;

Name: Anonymous 2007-02-12 10:39

>>3
so your point is?...

Name: Anonymous 2007-02-12 14:05

>>1
lol you're rewriting Common Lisp in Java? How stupid are you? Languages are supposed to make your life easier, not translate some concepts from one language to another lol

Name: Anonymous 2007-02-12 18:32

>>6
One word, the forced indentation of code. Thread over.

Name: Anonymous 2007-02-12 20:14 (sage)

>>7
Oh shuuut uuup.

Name: Anonymous 2007-02-13 0:51

>>9
no because this time I'm serious.

Name: Anonymous 2007-02-13 10:45

Dear OP, I was feeling generous and I wanted to piss off the 'DO YOUR OWN HOMEWORK' faggots, so I installed jdk and did your homework for you. Note that I've used null to mark the end of the list instead of inventing a magic singleton class. The main method acts as a test case/usage example.

public class ConsList //implements List
{
    private Object car;
    private ConsList cdr;

    public ConsList(Object car, ConsList cdr) {
        this.car = car;
        this.cdr = cdr;
    }

    public ConsList(Object car) {
        this.car = car;
        this.cdr = null;
    }

    public Object head() {
        return car;
    }

    public ConsList tail() {
        return cdr;
    }

    public boolean isLast() {
        return cdr == null;
    }

    public ConsList init() {
        if(isLast()) {
            return null;
        } else {
            return new ConsList(car, cdr.init());
        }
    }

    public Object last() {
        if(isLast()) {
            return car;
        } else {
            return cdr.last();
        }
    }

    public ConsList shiftRight(Object o) {
        return new ConsList(o, init());
    }

    public static void main(String[] args) {
        ConsList foo = new ConsList("hello", new ConsList("world",
                    new ConsList("java", new ConsList("sucks"))));

        for(ConsList bar = foo.shiftRight("omg"); bar != null; bar = bar.tail()) {
            System.out.println((String)bar.head());
        }
    }
}

Name: Anonymous 2007-02-13 10:51

>>11
why would we be pissed about that?

Name: Anonymous 2007-02-13 12:52

>>12
Because /prog/ is unhelpful.

Name: Anonymous 2007-02-13 13:04

>>13
that doesn't answer why would they be *pissed*, numbnuts

Name: Anonymous 2007-02-13 14:57

>>14
Don't spoil this for me :(

Name: Anonymous 2007-02-13 16:44

>>15
too late

Name: Anonymous 2009-02-25 7:48


Public boolean ms int?

Name: Anonymous 2010-06-25 14:47

WARNING: NECRO POST

Name: Anonymous 2010-11-16 4:26

Name: Anonymous 2010-12-20 22:33

Name: Sgt.Kabu庵kiman柸댕 2012-05-28 22:26

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List