d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java - New To Java, Unsure How To Start Objects
Add Reply New Topic New Poll
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 18 2014 12:21pm
Starting an assignment but unsure how to start it, don't want you guys to do it for me. Just need some help starting it

How would I create two objects, one being for a human and other being for a robot so I can make a robot vs robot game essentially?

Object for Robot
Object for Human
Retired Moderator
Posts: 18,570
Joined: Feb 27 2004
Gold: 11,858.00
Trader: Trusted
Sep 18 2014 12:27pm
Start by reading this: http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html

After you've read it post some code here and I'm sure you'll get more feedback.

This post was edited by Blind[zF] on Sep 18 2014 12:28pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 18 2014 12:35pm
Robot.java
Code
public class Robot {
private String name;

public Robot( String name ) {
this.name = name;
}

public String getName( ) {
return name;
}

public void setName( String name ) {
this.name = name;
}

}


Human.java
Code
public class Human {
private String name;

public Human( String name ) {
this.name = name;
}

public String getName( ) {
return name;
}

public void setName( String name ) {
this.name = name;
}

}


Main.java
Code
public class Main {
public static void Main( String[] args ) {
Robot robot = new Robot( "T1000" );
Human human = new Human( "Bob" );
}
}
Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 18 2014 12:40pm
Also what does this mean? "You will only need to program an application with a public static void main(String[] args) method" Does this mean only program with one main, or does that mean for what Labatymo put, i just insert that into the public class main?

Quote (Blind[zF] @ Sep 18 2014 01:27pm)
Start by reading this: http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html

After you've read it post some code here and I'm sure you'll get more feedback.


Okay I'll look at it after I finish classes! Currently in class till 3:30 or so

Quote (labatymo @ Sep 18 2014 01:35pm)
Robot.java
Code
public class Robot {
  private String name;

  public Robot( String name ) {
    this.name = name;
  }

  public String getName( ) {
    return name;
  }

  public void setName( String name ) {
    this.name = name;
  }
 
}


Human.java
Code
public class Human {
  private String name;

  public Human( String name ) {
    this.name = name;
  }

  public String getName( ) {
    return name;
  }

  public void setName( String name ) {
    this.name = name;
  }
 
}


Main.java
Code
public class Main {
  public static void Main( String[] args ) {
    Robot robot = new Robot( "T1000" );
    Human human = new Human( "Bob" );
  }
}


This is super helpful!
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 18 2014 12:58pm
Quote (Trev @ Sep 18 2014 02:40pm)
Also what does this mean?"You will only need to program an application with a public static void main(String[] args) method" Does this mean only program with one main, or does that mean for what Labatymo put, i just insert that into the public class main?


I'd have to see the assignment, but I guess your teacher just wants to start off simple by creating a main class with 2 sub-classes and a main method.

Code
public class Main {

static class Robot {
private String name;

public Robot( String name ) {
this.name = name;
}

public String getName( ) {
return name;
}

public void setName( String name ) {
this.name = name;
}

}

static class Human {
private String name;

public Human( String name ) {
this.name = name;
}

public String getName( ) {
return name;
}

public void setName( String name ) {
this.name = name;
}

}

public static void main( String[] args ) {
Robot robot = new Robot( "T1000" );
Human human = new Human( "Bob" );
}

}



or he could mean literally declare 2 abstract Objects called human and robot

Code
public static void main(String[] args){
Object robot;
Object human;
}

Member
Posts: 18,969
Joined: Aug 16 2007
Gold: 16,089.87
Sep 18 2014 03:29pm
I added an external .jar file in my project, do I need to import that on the top of my project or is it already imported or whatever you want to call it in my project without having to tell it to import it?

Also have robot.class and such, is that something I would have to import also or does that just use it, I'm so confused on how they are implemented or how to use them
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 19 2014 10:42am
Quote (Trev @ Sep 18 2014 05:29pm)
I added an external .jar file in my project, do I need to import that on the top of my project or is it already imported or whatever you want to call it in my project without having to tell it to import it?

Also have robot.class and such, is that something I would have to import also or does that just use it, I'm so confused on how they are implemented or how to use them


You have to add it to the build path. If you're using eclipse, right click the project, go to properties > build path > add external jar

After that, you can put import com.example.robot; (assuming that's the package name) at the top of your code to import it. Or an easier way is to define a Robot object, then hit ctrl+shift+O to automatically import the required package.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll