Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: Setekh on April 28, 2004, 08:32:07 am

Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 08:32:07 am
Hooray, Setekh is learning to do Object Oriented programming!

This code is simple, but pretty screwy because I still suck at this. Anyone's game to join in the discussion and help me out. I have to comment it a little better but that will come in a few minutes.

To compile, rename to *.java
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 08:43:11 am
Okay, fixed that problem (was just three letters poorly placed). Onwards and upwards now, I gotta add these cars to a list so I can manipulate them all at once...

To compile, rename to *.java
Title: Setekh's Annoying Java Project
Post by: kasperl on April 28, 2004, 08:55:22 am
does WinXP read java files out of the box? i get it in a notepad thingy, as code. Ugly without linebreaks.
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 09:03:57 am
Hmmm, well, this is what it looks like.

Code: [Select]
////////////////////////////////////////////////////////////////////////
//
//  Car
//
//  The objects and methods in this class simulate a car
//
//  Edward Woo
//  28 April 2004
//
////////////////////////////////////////////////////////////////////////

import java.util.Random;

public class Car
{
    private Random generator;
   
        private String model;
        // the car model (must be non-null)
   
    private int range;
        // the maximum range a new car can be from the park
       
    private int randomDistance;
        // the random distance from the park generated for a car, as within the range
       
    private int currentDistance;
        // the current distance a car has from the park

    public Car(String name)
        // constructor for a car with a given name
    {
        model = name;
        generator = new Random();
    }

    public String getModel()
        // returns the model of a car
    {
        return model;
    }
   
    public void assignDistance(int r)
        // assigns a random distance from the park to a car
        // @ param r the range that the car's random distance is generated within
    {
        range = r;
        randomDistance = 1 + generator.nextInt(range);
        System.out.println(getModel() + " begins " + randomDistance + "km from Park.");
        currentDistance = randomDistance;
    }
   
   


Edit: Hmmm, what happened to the rest of my code? It's in my post but it's not showing up...
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 09:11:34 am
And the rest of the code:

Code: [Select]
public void assignArrayDistances(int r)
        // traverses an array of cars and assigns a random distances to each car
        // @ param r the range that the cars' random distances are generated within
    {
        range = r;
        for (int i=0; i < cars.length; i++)
            {
                cars[i].assignDistance(r);
            }
    }
   
    public void printDistance()
        // prints a car's present distance to the screen
    {
        System.out.println(getModel() + "'s distance from Park is now " + randomDistance + "km.");
    }
   


    public static void main(String[] args){
    Car car1 = new Car("car1");
    car1.assignDistance(300);
    Car car2 = new Car("car2");
    car2.assignDistance(100);
    car1.printDistance();
    car2.printDistance();
    Car cars[] = {new Car("carA"),new Car("carB"),new Car("carC")};
    }
     
}
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 09:26:01 am
Updated:

Code: [Select]
////////////////////////////////////////////////////////////////////////
//
//  Car
//
//  The objects and methods in this class simulate a car
//
//  Edward Woo
//  28 April 2004
//
////////////////////////////////////////////////////////////////////////

import java.util.Random;

public class Car
{
    private Random generator;
   
        private String model;
        // the car model (must be non-null)
   
    private int range;
        // the maximum range a new car can be from the park
       
    private int randomDistance;
        // the random distance from the park generated for a car, as within the range
       
    private int currentDistance;
        // the current distance a car has from the park

    public Car(String name)
        // constructor for a car with a given name
    {
        model = name;
        generator = new Random();
    }

    public String getModel()
        // returns the model of a car
    {
        return model;
    }
   
    public void assignDistance(int r)
        // assigns a random distance from the park to a car
        // @ param r the range that the car's random distance is generated within
    {
        range = r;
        randomDistance = 1 + generator.nextInt(range);
        System.out.println(getModel() + " begins " + randomDistance + "km from Park.");
        currentDistance = randomDistance;
    }
   
    public void assignArrayDistances(int r, Car cars[])
        // traverses an array of cars and assigns a random distances to each car
        // @ param r the range that the cars' random distances are generated within
    {
        range = r;
        for (int i=0; i < cars.length; i++)
            {
                cars[i].assignDistance(r);
            }
    }
   
    public void printDistance()
        // prints a car's present distance to the screen
    {
        System.out.println(getModel() + "'s distance from Park is now " + randomDistance + "km.");
    }
   


    public static void main(String[] args){
    Car car1 = new Car("car1");
    car1.assignDistance(300);
    Car car2 = new Car("car2");
    car2.assignDistance(100);
    car1.printDistance();
    car2.printDistance();
    Car cars[] = {new Car("carA"),new Car("carB"),new Car("carC")};
    Car basecar = new Car(null);
    basecar.assignArrayDistances(10,cars[]);
    }
     
}
Title: Setekh's Annoying Java Project
Post by: phreak on April 28, 2004, 09:30:19 am
whats the objective of the assignment?  write a car class and do this:

Code: [Select]

    Car car1 = new Car("car1");
    car1.assignDistance(300);
    Car car2 = new Car("car2");
    car2.assignDistance(100);
    car1.printDistance();
    car2.printDistance();
    Car cars[] = {new Car("carA"),new Car("carB"),new Car("carC")};
    Car basecar = new Car(null);
    basecar.assignArrayDistances(10,cars[]);
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 09:36:24 am
The objective is to simulate cars going towards a Park and generating traffic and the like. It's starting off slowly. :p
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 10:28:05 am
Okay, this is going places. Update.

Code: [Select]
////////////////////////////////////////////////////////////////////////
//
//  Car
//
//  The objects and methods in this class simulate a car
//
//  Edward Woo
//  28 April 2004
//
////////////////////////////////////////////////////////////////////////

import java.util.Random;

public class Car
{
    private Random generator;
   
    private String model;
        // the car model (must be non-null)
   
    private int range;
        // the maximum range a new car can be from the park
       
    private int time;
        // the time waited for cars to move closer to the park
       
    private int randomDistance;
        // the random distance from the park generated for a car, as within the range
       
    private int currentDistance;
        // the current distance a car has from the park

    public Car(String name, int c)
        // constructor for a car with a given name
    {
        model = name;
        generator = new Random();
        currentDistance = c;
    }

    public String getModel()
        // returns the model of a car
    {
        return model;
    }
   
    public void assignRandomDistance(int r)
        // assigns a random distance from the park to a car
        // @ param r the range that the car's random distance is generated within
    {
        range = r;
        randomDistance = 1 + generator.nextInt(range);
        System.out.println(getModel() + " begins " + randomDistance + "km from Park.");
        currentDistance = randomDistance;
    }
   
    public void assignDistance(int d)
        // assigns a value to a car's distance
    {
        currentDistance = d;
    }

    public int getDistance()
        // returns a car's current distance
    {
        return currentDistance;
    }
   
    public int generateDistance(int r)
        // assigns a random distance from the park to a car
        // @ param r the range that the car's random distance is generated within
    {
        range = r;
        randomDistance = 1 + generator.nextInt(range);
        return randomDistance;
    }
   
    public void assignArrayDistances(int r, Car cars[])
        // traverses an array of cars and assigns a random distances to each car
        // @ param r the range that the cars' random distances are generated within
    {
        for (int i=0; i < cars.length; i++)
            {
                int p = generateDistance(r);
                cars[i].assignRandomDistance(p);
            }
    }
   
    public void wait(int time, Car cars[])
        // traverses an array of cars and assigns a random distances to each car
        // @ param r the range that the cars' random distances are generated within
    {
        for (int j=0; j < time; j++)
        {
            for (int i=0; i < cars.length; i++)
            {
                if(cars[i].getDistance()==0)
                    {
                    }
                else
                    {
                    int n = cars[i].getDistance() - 1;
                    cars[i].assignDistance(n);
                    }
            }
        }
    System.out.println(" ");    
    for (int i=0; i < cars.length; i++)
        cars[i].printDistance();
    }
   
   
    public void printDistance()
        // prints a car's present distance to the screen
    {
        if(currentDistance == 0)
            {
            System.out.println(getModel() + " has reached the Park!");
            }
        else
            {
            System.out.println(getModel() + "'s distance from Park is now " + currentDistance + "km.");
            }
    }
   


    public static void main(String[] args){
    // Car car1 = new Car("car1");
    // car1.assignDistance(300);
    // Car car2 = new Car("car2");
    // car2.assignDistance(100);
    // car1.printDistance();
    // car2.printDistance();

    Car cars[] = {new Car("carA",0), new Car("carB",0), new Car("carC",0),
    new Car("carD",0), new Car("carE",0), new Car("carF",0), new Car("carG",0), };
    Car basecar = new Car(null,0);
    basecar.assignArrayDistances(50,cars);
    // cars[0].printDistance();
    // cars[4].printDistance();
    // cars[3].assignDistance(19);
    // cars[3].printDistance();
    basecar.wait(5,cars);
    }
     
}
Title: Setekh's Annoying Java Project
Post by: Joey_21 on April 28, 2004, 10:29:06 am
Quote
Originally posted by kasperl
does WinXP read java files out of the box? i get it in a notepad thingy, as code. Ugly without linebreaks.


The version he posted is the ASCII human-readable code. To execute it you'll need to get Sun's Java SDK and compile the ASCII into binary, machine-readable code.

I never got to learn Java OOP, myself. Although, I am vastly familiar with C++ OOP. ;)
Java is what the Computer Science department at this uni taught as an introductory language. Trying to get the student familiar with the syntax, in general (Java and C++ syntax are similar). Then for most other classes, everything is done in C++.
Title: Setekh's Annoying Java Project
Post by: kasperl on April 28, 2004, 10:38:50 am
meh, no SDK's on this machine, and none are going to be on it either, i have to keep it "clean" or my dad will take it back........
Title: Setekh's Annoying Java Project
Post by: mikhael on April 28, 2004, 11:22:44 am
Quote
Originally posted by Setekh
The objective is to simulate cars going towards a Park and generating traffic and the like. It's starting off slowly. :p


Wait until they make you implement toll booths. Then you'll be doing my second to last java project.
Title: Setekh's Annoying Java Project
Post by: karajorma on April 28, 2004, 11:25:08 am
IIRC As long as you don't install the IE/netscape plugin the Java SDK won't do anything other than take up space on your hard drive :)

Been a while since I installed the Standard Edition SDK though :)
Title: Setekh's Annoying Java Project
Post by: Setekh on April 28, 2004, 11:49:41 am
Gah!!! I'm done. Good night all, talk to you in a few hours. ;)
Title: Setekh's Annoying Java Project
Post by: Flipside on April 28, 2004, 02:38:41 pm
Programmeritis already? ;)
Title: Setekh's Annoying Java Project
Post by: DragonClaw on April 28, 2004, 02:48:01 pm
A man of many skills, yes? :yes:
Title: Setekh's Annoying Java Project
Post by: Anaz on April 28, 2004, 03:32:28 pm
C++ > Java

:p
Title: Setekh's Annoying Java Project
Post by: ZylonBane on April 28, 2004, 05:20:53 pm
The first rule of Java is that JAVA SUCKS. It is, IMHO, insanely obsessed with scoping, to the point that you have to jam a laundry list of keywords in front of every mundane declaration.

JavaScript is (by design) a far more relaxing environment in which to learn object-oriented concepts. Everything is dynamic, object properties can be created and assigned at will, and variables are untyped. It's a shame there isn't a standalone version freely available (that I know of).
Title: Setekh's Annoying Java Project
Post by: mikhael on April 28, 2004, 07:51:22 pm
You're better off with python for that sort of thing ZB.

Almost everything is an object, everything is dynamic, object properties and methods can be added, removed or changed at any time, variables are untyped (mostly). Functions are first class objects, and its fully interactive.

Best of all, its freely available for all platforms.
Title: Setekh's Annoying Java Project
Post by: ZylonBane on April 28, 2004, 11:42:20 pm
I've heard Python is good, but starting on Python just when I'm starting to get halfway competent at Perl would probably just confuse me.
Title: Setekh's Annoying Java Project
Post by: mikhael on April 29, 2004, 01:22:46 am
Oh no. You definately don't want to try to learn the two at the same time. Just look out, if you do decide to learn python, white space is semantically signifigant. Its no big deal if you're consistent with your indent style, but it can bite you on the ass if you're not careful.

Of course, the exception trace you get from python is pretty damned helpful when it comes to finding errors like that. :D
Title: Setekh's Annoying Java Project
Post by: Setekh on April 29, 2004, 05:49:15 am
My brother officially says of Python and Perl: "If you've already started, learn Perl and use it for 6 months. Then move onto Python and never look back - everything you want to do in Perl you can do in Python, and there are less semantic traps you can fall into." ;)
Title: Setekh's Annoying Java Project
Post by: mikhael on April 29, 2004, 02:43:23 pm
There's one thing Perl has over python: regular expressions. The Perl regex mechanism is one of the finest I've ever seen and far easier than the Python mechanism.

I liked a quote a read somewhere about Python v. Perl: If Python is executable psuedocode, Perl is executable line noise... :D