Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Arrays using parameters in JAVA?


  • Please log in to reply
7 replies to this topic

#1 sausage

sausage

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:11:53 AM

Posted 30 November 2008 - 09:18 PM

Ok.... so.... without arrays, is it possible to make multiple instances of one class? I don't think so and if I can't please answer my next question.

I am trying to make an RPG using object-oriented programming in JAVA

import java.util.*;

public class BattleCall
{
	 public static void main(String[] args)
	 {
		  State h = new State();
		  Monster[] monsters = new Monster[1];//Not sure if this is necessary
		  Monster[] Goblin = new Monster[2];//Here is where I get confused
		  Monster[] Pirate = new Monster[3];
		  h.h(Goblin,Goblin)
	 }
}

Alright: Explanation: The Monster class has multiple variables that are changed through a constructor, with the array, I have NO idea where to place parameters for the constructor. Without the array, my code would look something like

//......Beggining stuff here
State h = new State();
Monster Goblin = new Monster(12,10,12,1,8,-1,13,1,8,2);//The parameters, first number on both instances is the initialization of the variable "AC"
Monster Pirate = new Monster(14,11,13,1,9,-1,15,2,10,3);
h.h(Goblin,Goblin)
//Ending stuff here, no extra variables


New explanation: What this does is creates two instances of the same class which I don't think is possible, especially because of the output I get. Oh, here is the code for the State class and Monster class.

//....Begginning stuff here.....again
int AC;
//.....Ending stuff including whatever other variables are in here.... not used yet....

That's the monster class
Here's the State class

import java.util.*;

public class State
{
	 public void h(Monster mon1, Monster mon2)
	 {
		  System.out.println(mon1.AC+","+mon2.AC);
	 }
}

When this program is run without the arrays, it only shows 14,14 (the pirate's AC) even though I told it to say the goblin's AC (12)

Those of you that think it is an error with my variable initialization I promise you, pirate is 14 and goblin is 12.

Final Point: What I need to do is use an array to be able to create multiple instances of the Monster class while still using the constructors to change the variables inside it.

PLEASE HELP!!!

Edited by sausage, 30 November 2008 - 09:21 PM.


BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:53 AM

Posted 30 November 2008 - 10:12 PM

Why don't you just use a for loop?

I'm not up too much on Java... but cant you do something like:

Monster MonsterArray[6];
for(int idx = 0; idx <= 6; idx++) {
:thumbsup: MonsterArray[idx] = new Monster(some stuff here);
}

MonsterArray now contains 7 monster instances

Billy3

Edited by Billy O'Neal, 30 November 2008 - 10:13 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:11:53 AM

Posted 30 November 2008 - 10:24 PM

Monster MonsterArray[6];
for(int idx = 0; idx <= 6; idx++) {
MonsterArray[idx] = new Monster(some stuff here);
}

That's a really good idea, unfortuanately, the error I get is on Monster MonsterArray[6], it says "]" expected.........

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:53 AM

Posted 30 November 2008 - 11:07 PM

I'm not entirely sure how that's done in java. How would you normally declare a 7 element array?

In C it is:
Type name[6];

EDIT:
i.e.
string name[6]; //Allocate an array of seven strings

Billy3

Edited by Billy O'Neal, 30 November 2008 - 11:07 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:11:53 AM

Posted 30 November 2008 - 11:35 PM

Well, I got the array part of it but I ran into the same problem, I can't seem to make two instances of the same object with different variables..... If you know how to do that I would be happy.

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:53 AM

Posted 04 December 2008 - 08:17 AM

Your objects need a constructor that takes some parameters then.

MonsterArray[] mn = new MonsterArray[];
mn[1] = new Monster(1,2,3,4);
mn[2] = new Monster(2,3,4,5);

Then in your monster class, you need some sort of a constructor that takes the parameters, and assigns them to an instance variable.

@Billy, when you create an array of size 6, then it has 6 elements, not 7. It would be indexed as zero through five. If you want a 7 element array, then you would construct it using 7.

This code
String[] st = new String[6];
		
		//System.out.println(st);
		for(int i=0; i<st.length; i++){
			System.out.println(st[i]);
		}

will print out 6 nulls. Not 7.




That's a really good idea, unfortuanately, the error I get is on Monster MonsterArray[6], it says "]" expected.........

Then you have a syntax error in your code, and you didn't use the code that Billy gave you because his code has no syntax errors.

#7 sausage

sausage
  • Topic Starter

  • Members
  • 389 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:11:53 AM

Posted 04 December 2008 - 07:30 PM

Thanks Groovicus but I had figured this out a while ago, and I am no longer using arrays but thanks for trying :-) I appreciate it

#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:53 AM

Posted 04 December 2008 - 07:37 PM

Yeah.. didnt' realize until afterward... forgot where I read somewhere that what's in [] was the max index rather than the # of elements.. guess I'll go back into my corner now :thumbsup:

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users