Java help needed.

Status
Not open for further replies.
Hey. Ill try and help you here so that I put you in the right direction but not give you the full answer right out.
Yeah, that's good, because then I understand this stuff.

For that first question you want to create an object and when you create that object you pass it the parameters inside the parenthesis that get sent to the constructor.
So, I'm not quite sure how to do the parameters. I sort of got the rest, but that confuses me.
Code:
Electron elecOne = new Electron('u' (and from there I don't know quite what to do))

Now for the second question. Is there another method to be able to set the speed or is passing the speed to the constructor via constructing an object the only way? If you can only pass the speed to the constructor then there is no other way and the answer is no.
I don't quite understand. So, is the answer no because you would have to create an object to do this?


Fourth question. This is a static method. Are you allowed to access non-static variables (xDir) inside a static method?
No. I don't know why I didn't see that.

Seventh question. So there are three objects being created. Each making the electronCount go up by one. So then it prints the electronCount variable. How many times did it increase by?
For the last line of code in question seven. is Electron an object that was created? If not, can you call the electronCount method with a non-existant object?
I have no idea what you said or what to do here.
 
Electron elecOne = new Electron('u' (and from there I don't know quite what to do))

Ok you got the first part. Then what I believe they mean by the 3i + 4j - 5k is that i = 3.0, j = 4.0, k = -5.0. I got that because i(1,0,0) j(0,1,0) k(0,0,1).
Also for the speed of electron = 2.6E6 (I think thats how sci notation is for java....)

So you would go.....
Electron elecOne = new Electron('u', 2.6E6, 3.0, 4.0, -5.0);

I hope thats sorta clear.


I don't quite understand. So, is the answer no because you would have to create an object to do this?

Yes. That is correct. You have to construct an object so you can pass the constructor parameters like the speed.

I have no idea what you said or what to do here.

Code:
[COLOR="Red"]Electron elec1 = new Electron('u', 2.6E6, 3, 4, -5);
Electron elec2 = new Electron('d', 2.7E6, 3, 5, -7);
Electron elec3 = new Electron('u', 1.6E6, 3. -6. -5);[/COLOR]
System.out.println(elec1.electronCount);
System.out.println(elec2.electronCount);
System.out.println(elec3.electronCount);
System.out.println(Electron.electronCount);

So there are three objects being constructed (in red above). each time one is constructed the constructor is run

constructor
Code:
public Electron(char spn, double s, double i, double j, double k) 
     {
          spin = spn;
          speed = s;
          xDir = i;
          yDir = j;
          zDir = k;
          [COLOR="Lime"]electronCount++[/COLOR]; 
     }

In turn the electronCount++ (in green above) is increased by one.

Code:
[COLOR="Yellow"][COLOR="Orange"]System.out.println(elec1.electronCount);
System.out.println(elec2.electronCount);
System.out.println(elec3.electronCount);[/COLOR][/COLOR]
System.out.println(Electron.electronCount);


Then it calls the electronCount and displays what the count is (in orange). The last line of code above will not retrieve the electronCount because Electron is not an object that was created.

I hope that all makes sense and that im right also ;)
 
Okay, all these other should be simple, I just don't really get how to do any of it. I really should go back over these last few lessons, if I had the time.

8) Write code to create a wrappe class equivalent of int j and store it in Integer jj

9) Write a line of code that will convert String s into an int type. Store the results in an integer variable called pk

10) Write a line of code that will convert to integer jp into a String equivilant

11) Write code that will create a Boolean type wrapper object and "wrap" false in it. Call the object ob.

12) Write a single line of code that shows how to convert the wrapper class Integer iw into an int called j

13) Suppose you have the following code:
Code:
String sd = "3.14159";
String si = "10";
Write code that will convert these two Strings into numeric types, multiply them, and then store the result in double dd

14) __________ type classes convert primitive data types into object equivalents

15) Suppose that cw is a Character type object. Wrie a single line of code that will print the character "wrapped" inside the object

16) What is the output of the following code?
Code:
public class Tester
{
   static public void main()
   {
      int p = 3, q = 1;
      Integer iw[] = new Integer[10]
      while ( q < p)
      {
         iw[q] = q*2;
         q++;
      }
      q = 1;
      while (q < p)
      {
         System.out.print(iw[q]);
         q++;
      }
      System.out.println(q);
   }
}

17) Suppose you have a String s that contains a representation of an integer (456). How would you convert this into a String that would be the hex equivalent of that integer. (Hint, first convert s into an int then convert this to a hex equivalent String.

18) Convert the String ns that contains "92.82" into a numeric.

19) Suppose you have a BankAccount class that has a variable called accountNumber. Each time the constructer runs,it increments this variable and assigns the new value to the state variable custNumber. Thus, custNumber will be a unique number for each new account that is opened. Show how you would declare the class variable accountNumber so that is would behave as described here.

20) Show how to convert 23 into its wrapper class equivalent.



Again, any help would be awesome.
 
I really should go back over these last few lessons, if I had the time.
And you really think posting questions on an internet forum and waiting for answers is faster? Maybe you should spend your time more wisely and take your own advice.
 
Status
Not open for further replies.
Back
Top Bottom