Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-15-2008, 06:00 PM   #1 (permalink)
 
Super Techie

Join Date: Feb 2008

Posts: 302

Thorn is on a distinguished road

Default Java help needed.

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
Thorn is offline  
Old 04-15-2008, 07:37 PM   #2 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Java help needed.

(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?
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz

Last edited by countrydude; 04-15-2008 at 07:55 PM.
countrydude is offline  
Old 04-15-2008, 11:05 PM   #3 (permalink)
 
Super Techie

Join Date: Feb 2008

Posts: 302

Thorn is on a distinguished road

Default Re: Java help needed.

Quote:
Originally Posted by countrydude View Post
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, .......
Thorn is offline  
Old 04-16-2008, 05:40 AM   #4 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Java help needed.

EDIT: Very good point jaeusm. My bad. I noticed that last time THorn was active was after I posted this so he hasnt seen it.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz

Last edited by countrydude; 04-16-2008 at 04:02 PM.
countrydude is offline  
Old 04-16-2008, 08:18 AM   #5 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Java help needed.

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.
jaeusm is offline  
Old 04-16-2008, 04:06 PM   #6 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Java help needed.

Quote:
Originally Posted by Thorn View Post
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.....


Quote:
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.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz
countrydude is offline  
Old 04-16-2008, 07:29 PM   #7 (permalink)
 
Super Techie

Join Date: Feb 2008

Posts: 302

Thorn is on a distinguished road

Default Re: Java help needed.

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.
Thorn is offline  
Old 04-16-2008, 08:01 PM   #8 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Java help needed.

Cool that you got it working fine.

Your welcome.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz
countrydude is offline  
Old 04-18-2008, 06:53 PM   #9 (permalink)
 
Super Techie

Join Date: Feb 2008

Posts: 302

Thorn is on a distinguished road

Default Re: Java help needed.

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);

Thorn is offline  
Old 04-19-2008, 01:55 PM   #10 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Java help needed.

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.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz

Last edited by countrydude; 04-19-2008 at 01:59 PM.
countrydude is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed with java dnoch Programming Discussions 1 09-09-2007 06:43 AM
Dangerous Java Flaw Threatens Virtually Everything Osiris Virus - Spyware Protection / Detection 1 07-13-2007 04:44 PM
hacking online java program. Z e i g Browser & General Internet Questions 8 06-23-2007 01:50 AM
Java Game Yek Programming Discussions 5 06-16-2007 04:29 AM
Java SCript Help NEEDED! kellee91 Browser & General Internet Questions 1 06-05-2007 12:20 PM