Java help needed.

Status
Not open for further replies.

Thorn

In Runtime
Messages
324
So, I have these projects in my programming class, and I have no clue how to do some of them. (I don't see the teacher again until next week, so I can't ask him)


"The main method will use two for loops.

The first will produce an integer count from 65 to 90 (ASCII 'A' - 'Z') and initialize the elements of the character ch[] with the characters corresponding to the ASCII codes generated by the loop.

The second loop will print the 26 elements of the ch[] array with a comma followed by a space between adjacent characters."


Help? (way too much work to just print out A, - Z
 
(sigh! I had the explanation written out and I accidently erased it. Here I go again.)

Ok here is an overview first.

I created a for loop to store int's into an array then used another for loop to convert int's to character's and printed them.

First I created the array with 26 elements of type int.

-I stored 65 into a variable. (used below)

-Then created the first for loop to count from 0-25 (The array elements). Then in the for loop it stores the variable with 65 in it into the first element of the array. Then increased the variable with the int 65 in it by 1.
Thus when the loop is done it has the numbers from 65 to 90 stored into each element of the array.

-Then created the second for loop. In the for loop it accesses each element in the array and converts it into the character from the int. I stored that conversion into a variable of type char. Then I just printed the variable. This is all done inside the second for loop.

Well I hope this explanation helps. I do have code if you like to see how it works because I did code it so I new what i was saying was gonna work.

p.s. I did it by storing int's into the array. Or did your teacher want characters stored in the array?
 
Well I hope this explanation helps. I do have code if you like to see how it works because I did code it so I new what i was saying was gonna work.

p.s. I did it by storing int's into the array. Or did your teacher want characters stored in the array?

I would love to see the code, but I don't just want to copy it. Thanks for the explaination, definately helped a bit.

I think he wanted the charachers, because what's printed is:

A, B, C, .......
 
Why would you post your code? Why give the answer straight away? Thorn's teacher doesn't want to evaluate you, he wants to evaluate Thorn. If you want to help, then you let the guy with the question post the code he has written and help him with that.
 
I would love to see the code, but I don't just want to copy it. Thanks for the explaination, definately helped a bit.

Hey Thorn. Jaeusm had a good point of not providing you the source code. But anyway.....


I think he wanted the charachers, because what's printed is:

A, B, C, .......

What I mean is that I stored int's into the array and then converted them to the ascii characters instead of converting the int's into ascii characters and then storing them into the array.

If you got part of the program coded but is having problems then you can post that and Ill help you with that.
 
I got it worked out.


Programmed it in C++ at one in the morning.

Quite funny. The hard part was converting some of it to Java.


Thanks.
 
Right, so I'm two units behind, and I completly bombed the test because of it. I need to make it up to pass. I would love to understand this stuff, but I don't know if I can, because I don't understand the stuff leading up to it. So, there is a lot(20 questions), and any help would be awesome.

Straight from the test:
Code:
Public class Electron 
{
     public Electron(char spn, double s, double i, double j, double k) 
     {
          spin = spn;
          speed = s;
          xDir = i;
          yDir = j;
          zDir = k;
          electronCount++; 
     }   
        //notice the mass changes with speed
     public static double getMass() 
     {
          double m = restMass / Math.sqrt(1 - speed * speed / ( c * c))
          return m; 
     }

//other methods not shown

     public static final double restMass = 9.11E-31;
     public static final double charge = 1.6E-19;
     public static final double c - 3E8; //speed of light
     public double speed;
     private double xDir;
     private double yDir;;
     private double zDir;
     private char spin;
     public static int electronCount = 0;
}

1) Write a line of code that will create an Electron object called elecOne. Pass the constructor a spin value of 'u' and a speed of '2.6 X 10^6' m/sec. The direction of the electron is given by the following vector: 3i + 4j - 5k

2) Suppose you are writing code for the main method of a Tester class and you do not want to create and object; yet you want to find the mass (not rest mass) of an electron speeding along at 2.5 X 10^8 m/sec. Write code that will allow you to pass this speed to the class and then print the resulting mass of the electron without creating an object. Is this possible? If so, write the code. If not, state why.

3) Suppose you are creating a new class and there is a need to set the variable e equal to mc^2, where m is the rest mass of an electron and c is the speed of light. Write a line of code that does this in which you obtain m and c directly from the Electron class without creating and instance of the class.

4) Suppose the following method is added to the Electron class:
Code:
Public static double calculate(int x)
     {
          return xDir * x;
     }
Comment on the legality of this method.

5) Suppose the following method is added to the Electron class:
Code:
Public double calculate(int x)
     {
          return xDir * x;
     {
Comment on the legality of this method.

7) Suppose the following code is in the main method of a Tester class. What is the output?
Code:
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);
System.out.println(elec1.electronCount);
System.out.println(elec2.electronCount);
System.out.println(elec3.electronCount);
System.out.println(Electron.electronCount);
 
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.

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.

ex. [class name] [object name] = new [class name](parameters);
ex. theclass theobject = new theclass('t', 4, 6, 5);

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.

Third question. Umm not exactly sure....

Fourth question. This is a static method. Are you allowed to access non-static variables (xDir) inside a static method?

Fifth question. I think that code is legal....I could be wrong though.

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?

IF there is anybody out there that would like to double check me that would be great! Just to make sure im not misleading anybody.
 
Status
Not open for further replies.
Back
Top Bottom