Exam Revision

please can someone help me with this

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

47 Replies - 2705 Views - Last Post: 11 May 2009 - 09:27 PM Rate Topic: -----

#1 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Exam Revision

Posted 27 April 2009 - 08:03 AM

Hi there,

I've got my programming exam coming up and just need someone to quickly run through what this program is doing:

import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class NumbersApp extends Object
{

public static void main(String[] argStrings) throws Exception
 {
 
ArrayList<Double> sn = new ArrayList<Double>();
Scanner t = new Scanner(new File("../files/input.txt"));
while (t.hasNextDouble())
{
double v = t.nextDouble();
System.out.println("bb: " + v);
sn.add(v);
}
t.close();
for (int a = 1; a < sn.size() - 1; a++)
{
System.out.println(sn.get(a));



}
 }
}


this is a revision question, the questions are:
seperate the output streams (but theres only one right??)
explain what type of variables sn and t are
Suggest better variable names for sn, t, v, and a.
Are all the items in the file printed? If not, modify the code to print all the items. (if I modify the "for" line to 0 instead of 1, it will print them)
Modify the code to print only those numbers which are not negative.

as I said I just need someone to quickly run through with me what this code is doing then i'll be able to answer the questions.

any help is greatly appriciated!

Thanks

This post has been edited by Addio569: 27 April 2009 - 08:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Exam Revision

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Exam Revision

Posted 27 April 2009 - 08:05 AM

Why don't you take a shot at it and we can help you afterward? You learn a lot more correcting mistakes than being given the answer.
Was This Post Helpful? 0
  • +
  • -

#3 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 27 April 2009 - 08:08 AM

View PostWilliam_Wilson, on 27 Apr, 2009 - 07:05 AM, said:

Why don't you take a shot at it and we can help you afterward? You learn a lot more correcting mistakes than being given the answer.


I've got it to run, just need to know what the lines are doing, without knowing this how is it possible to rename variables, modify etc??
Was This Post Helpful? 0
  • +
  • -

#4 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 27 April 2009 - 08:18 AM

View PostAddio569, on 27 Apr, 2009 - 07:08 AM, said:

View PostWilliam_Wilson, on 27 Apr, 2009 - 07:05 AM, said:

Why don't you take a shot at it and we can help you afterward? You learn a lot more correcting mistakes than being given the answer.


I've got it to run, just need to know what the lines are doing, without knowing this how is it possible to rename variables, modify etc??


to make myself clearer, I'm not being lazy, just need some more understanding.

ArrayList<Double> sn = new ArrayList<Double>();


I dont know what this line is doing

Scanner t = new Scanner(new File("../files/input.txt"));


I know that this line is a scanner and will read from text file "input.txt" which I made and put into "files"

while (t.hasNextDouble())


not sure what this is doing, think it tells the program to continue reading after each line

double v = t.nextDouble();


Havent a clue what this is doing lol

System.out.println("bb: " + v);


this prints to the command window bb: and adds the variable "v" to the line.

sn.add(v);


again, havent a clue what this is doing!!

t.close();


close after reading from file??

for (int a = 1; a < sn.size() - 1; a++)


a for loop, execute through starting from seconnd number in index, not sure what its doing after int a=1;

System.out.println(sn.get(a));


prints to command window, but not sure what, maybe the text from input.txt???

if anyone is curious input.txt is:

234.5
104.5
-90.8
908.765

if anyone could clear up the bits im not too sure of (or if I am not understanding correctly) i would greatly appriciate help.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Exam Revision

Posted 27 April 2009 - 08:25 AM

sn is of type arraylist, you can look up the java documentation to learn that the add method on an array list places the element to the end of the list.

You can apply similar logic to learn what most of the rest do as well.

*Hint: the Scanner class methods will help you with the nextDouble() method calls.

I would assume you should be able to understand most of it on your own since it is an exam review.
Was This Post Helpful? 0
  • +
  • -

#6 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 27 April 2009 - 08:35 AM

View PostWilliam_Wilson, on 27 Apr, 2009 - 07:25 AM, said:

sn is of type arraylist, you can look up the java documentation to learn that the add method on an array list places the element to the end of the list.

You can apply similar logic to learn what most of the rest do as well.

*Hint: the Scanner class methods will help you with the nextDouble() method calls.

I would assume you should be able to understand most of it on your own since it is an exam review.


Quote

I would assume you should be able to understand most of it on your own since it is an exam review.


Thats a fair call, but its not the only thing we've had to understand! its fair enough saying that I can apply similar logic to understand the rest, but do you think i'd be asking for help if I could apply logic??

I just need someone to help me understand the lines that I have pointed out that I have no clue about, a maximum of 4 lines!

I thought this was a forum where people help people?? I don't need a lecture, I need help!

I you haven't got anything productive to say then don't say it! I've been on this forum numerous times and each time someone has had the decency to help, unlike you sir!

This post has been edited by Addio569: 27 April 2009 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Exam Revision

Posted 27 April 2009 - 08:57 AM

To be fair to William…this is pretty simple code, you could find most of your questions in the APIs, and if you're just now getting around to trying to figure it out after you've learned it in class then you kind of deserve not to have any help.

To be merciful to Addio…we've all crammed at some point before an exam, and we've all had things that don't click no matter how hard we bang it against our heads. So I'll give some more assistance.


sn is an ArrayList of type double. In other words, it's one object to hold a bunch of doubles in indexes (starting with 0, 1, 2…). The first line is creating the list with the word "new".

You're close on the "while" statement…it's telling the Scanner to check the file and find where the next double is, and do the stuff afterwards for as long as it can find another double.

The next line tells the Scanner to check which double comes next in the File and save it to the double "v".

Then the double "v" gets added to the end of the ArrayList sn.

t.close refers to severing ties between the program and the File. It makes sure everything is clean in between etc. so that Files are not damaged.

The "for" loop says while "a" is less than the size of the ArrayList sn, run through each double in sn and print it out. Because "a" starts at 0, and the ArrayList starts counting the indexes of the things inside it at 0, the "for" loop first checks sn at point 0, then at point 1, then at point 2, etc.

Get it? :)

Edited for errors…

This post has been edited by SayMoi: 27 April 2009 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#8 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 27 April 2009 - 09:07 AM

View PostSayMoi, on 27 Apr, 2009 - 07:57 AM, said:

To be fair to William…this is pretty simple code, you could find most of your questions in the APIs, and if you're just now getting around to trying to figure it out after you've learned it in class then you kind of deserve not to have any help.

To be merciful to Addio…we've all crammed at some point before an exam, and we've all had things that don't click no matter how hard we bang it against our heads. So I'll give some more assistance.


sn is an ArrayList of type double. In other words, it's one object to hold a bunch of doubles in indexes (starting with 0, 1, 2…). The first line is creating the list with the word "new".

You're close on the "while" statement…it's telling the Scanner to check the file and find where the next double is, and do the stuff afterwards for as long as it can find another double.

The next line tells the Scanner to check which double comes next in the File and save it to the double "v".

Then the double "v" gets added to the end of the ArrayList sn.

t.close refers to severing ties between the program and the File. It makes sure everything is clean in between etc. so that Files are not damaged.

The "for" loop says while "a" is less than the size of the ArrayList sn, run through each double in sn and print it out. Because "a" starts at 0, and the ArrayList starts counting the indexes of the things inside it at 0, the "for" loop first checks sn at point 0, then at point 1, then at point 2, etc.

Get it? :)

Edited for errors…


thanks that makes a lot of sense!
Was This Post Helpful? 0
  • +
  • -

#9 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Exam Revision

Posted 27 April 2009 - 09:19 AM

View PostAddio569, on 27 Apr, 2009 - 04:07 PM, said:

thanks that makes a lot of sense!


Great! :) Glad I could help. I've had people who saved my skin when they really shouldn't have…so just pass the favour on sometime. :) Good luck on the exam!
Was This Post Helpful? 0
  • +
  • -

#10 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Exam Revision

Posted 27 April 2009 - 10:23 AM

you need to do now is understand the code and what it does its pretty simple, you just need to modify the code to just print out the numbers that arnt negative. part 4 of the question doesnt count as it means nothing caus it does already print everything out.

what you need to do is work out how to print positve numbers

This post has been edited by seaneyb: 27 April 2009 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#11 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 28 April 2009 - 06:00 AM

View Postseaneyb, on 27 Apr, 2009 - 09:23 AM, said:

you need to do now is understand the code and what it does its pretty simple, you just need to modify the code to just print out the numbers that arnt negative. part 4 of the question doesnt count as it means nothing caus it does already print everything out.

what you need to do is work out how to print positve numbers


ok, how do I do this?

Do I need to do some sort of sort? or like this?

if (numbers > negative);


I don't what I'm doing! aggggh!
Was This Post Helpful? 0
  • +
  • -

#12 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Exam Revision

Posted 28 April 2009 - 07:02 AM

View PostAddio569, on 28 Apr, 2009 - 05:00 AM, said:

View Postseaneyb, on 27 Apr, 2009 - 09:23 AM, said:

you need to do now is understand the code and what it does its pretty simple, you just need to modify the code to just print out the numbers that arnt negative. part 4 of the question doesnt count as it means nothing caus it does already print everything out.

what you need to do is work out how to print positve numbers


ok, how do I do this?

Do I need to do some sort of sort? or like this?

if (numbers > negative);


I don't what I'm doing! aggggh!

its pretty simple mate, ask mike or mel to go thru it with you....

this is what it should be


				 if (Number >0)
				 // if the object in the array is greater than 0 then print out.......

		{
			System.out.println("Numbers: " + Number);
		}

simple as pie mate.

always use if statements if your looking for a specific value or term eg... even & odd ;)
have a look through the tutorials on here they will help you understand the different loops and what you can get from using specific ones.

the 3 basic ones are for if and while loops you will need to know this for your exam mate.
Was This Post Helpful? 0
  • +
  • -

#13 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 28 April 2009 - 07:08 AM

View Postseaneyb, on 28 Apr, 2009 - 06:02 AM, said:

View PostAddio569, on 28 Apr, 2009 - 05:00 AM, said:

View Postseaneyb, on 27 Apr, 2009 - 09:23 AM, said:

you need to do now is understand the code and what it does its pretty simple, you just need to modify the code to just print out the numbers that arnt negative. part 4 of the question doesnt count as it means nothing caus it does already print everything out.

what you need to do is work out how to print positve numbers


ok, how do I do this?

Do I need to do some sort of sort? or like this?

if (numbers > negative);


I don't what I'm doing! aggggh!

its pretty simple mate, ask mike or mel to go thru it with you....

this is what it should be


				 if (Number >0)
				 // if the object in the array is greater than 0 then print out.......

		{
			System.out.println("Numbers: " + Number);
		}

simple as pie mate.

always use if statements if your looking for a specific value or term eg... even & odd ;)
have a look through the tutorials on here they will help you understand the different loops and what you can get from using specific ones.

the 3 basic ones are for if and while loops you will need to know this for your exam mate.


ah cheers mate! how the hell did you know I was at bournemouth?? lol! I asked mike and guess what? no help! thanks again, that was very simple! what tutorials are good on here then? also what book do you reccomend reading?

cheers again!
Was This Post Helpful? 0
  • +
  • -

#14 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Exam Revision

Posted 28 April 2009 - 07:33 AM

i go to bournemouth too haha! i knew straight away when i saw the code.

ahh mike is cool, mel is good just go ask her dont ask heather tho shes useless and always grumpy!

just have a look at the stuff thats pinned up its helped me out alot and just give things a shot mate, if what u think doesnt run and compile wack it on here and people do help they just wont tell u straight up just gotta use ur nossle.

erm java for dummies is good i downloaded that the like first week i was here, had some good examples and was easy to understand, but i think that lewis and lofus books is what they reccomend.

This post has been edited by seaneyb: 28 April 2009 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#15 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: Exam Revision

Posted 28 April 2009 - 09:18 AM

View Postseaneyb, on 28 Apr, 2009 - 06:33 AM, said:

i go to bournemouth too haha! i knew straight away when i saw the code.

ahh mike is cool, mel is good just go ask her dont ask heather tho shes useless and always grumpy!

just have a look at the stuff thats pinned up its helped me out alot and just give things a shot mate, if what u think doesnt run and compile wack it on here and people do help they just wont tell u straight up just gotta use ur nossle.

erm java for dummies is good i downloaded that the like first week i was here, had some good examples and was easy to understand, but i think that lewis and lofus books is what they reccomend.


cool, just got the java for dummies now!

i'm now stuck on this code:

int[] vn = {427, 649, 648, 696, 109};
int ma = 0;
for (int cg = vn.length - 1; cg >= 0; cg--)
{
System.out.println("j: " + vn[cg]);
ma = ma + vn[cg];
}
System.out.println(ma);


its asking me to modify the code to only include the numbers over 500. and maybe add them up.

i put:

 
for (int cg = vn.length - 1; cg >= 0; cg--)
if (numbers>500)
{
System.out.println("j: " + vn[cg]);
ma = ma + vn[cg];
}
System.out.println(ma);



didn't work, I got an error, it worked for the double but it says it wont work with an int. what am I doing wrong?
also is the variable "cg" i? is it always going to be an i because thats all i've ever put!

Thanks

This post has been edited by Addio569: 28 April 2009 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »