Author Topic: Setekh's Annoying Java Project  (Read 1573 times)

0 Members and 1 Guest are viewing this topic.

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 
Setekh's Annoying Java Project
does WinXP read java files out of the box? i get it in a notepad thingy, as code. Ugly without linebreaks.
just another newbie without any modding, FREDding or real programming experience

you haven't learned masochism until you've tried to read a Microsoft help file.  -- Goober5000
I've got 2 drug-addict syblings and one alcoholic whore. And I'm a ****ing sociopath --an0n
You cannot defeat Windows through strength alone. Only patience, a lot of good luck, and a sledgehammer will do the job. --StratComm

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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...
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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")};
    }
     
}
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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[]);
    }
     
}
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
Setekh's Annoying Java Project
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[]);
Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
The objective is to simulate cars going towards a Park and generating traffic and the like. It's starting off slowly. :p
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
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);
    }
     
}
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Joey_21

  • 28
    • http://denebsystem.cjb.net/
Setekh's Annoying Java Project
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++.
« Last Edit: April 28, 2004, 10:34:00 am by 34 »

 
Setekh's Annoying Java Project
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........
just another newbie without any modding, FREDding or real programming experience

you haven't learned masochism until you've tried to read a Microsoft help file.  -- Goober5000
I've got 2 drug-addict syblings and one alcoholic whore. And I'm a ****ing sociopath --an0n
You cannot defeat Windows through strength alone. Only patience, a lot of good luck, and a sledgehammer will do the job. --StratComm

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
Setekh's Annoying Java Project
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.
[I am not really here. This post is entirely a figment of your imagination.]

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Setekh's Annoying Java Project
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 :)
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Setekh

  • Jar of Clay
  • 215
    • Hard Light Productions
Setekh's Annoying Java Project
Gah!!! I'm done. Good night all, talk to you in a few hours. ;)
- Eddie Kent Woo, Setekh, Steak (of Steaks), AWACS. Seriously, just pick one.
HARD LIGHT PRODUCTIONS, now V3.0. Bringing Modders Together since January 2001.
THE HARD LIGHT ARRAY. Always makes you say wow.

 

Offline Flipside

  • əp!sd!l£
  • 212
Setekh's Annoying Java Project
Programmeritis already? ;)

 

Offline DragonClaw

  • Romeo Kilo India Foxtrot
  • 210
Setekh's Annoying Java Project
A man of many skills, yes? :yes:

 

Offline Anaz

  • 210
Setekh's Annoying Java Project
C++ > Java

:p
Arrr. I'm a pirate.

AotD, DatDB, TVWP, LM. Ph34r.

You WILL go to warpstorm...

 

Offline ZylonBane

  • The Infamous
  • 29
Setekh's Annoying Java Project
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).
ZylonBane's opinions do not represent those of the management.

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
Setekh's Annoying Java Project
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.
[I am not really here. This post is entirely a figment of your imagination.]

 

Offline ZylonBane

  • The Infamous
  • 29
Setekh's Annoying Java Project
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.
ZylonBane's opinions do not represent those of the management.