Quantcast

Maximum PC

It is currently Wed Sep 17, 2014 9:15 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 58 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Starting out Mang
PostPosted: Sun Nov 02, 2008 3:52 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
mmkay so I had finished that BASIC programming class last year (basically a free credit lol sad i know.

This is the thread I will keep my "notes* and 'struggles' I have in AP comp Science AB

It seems I have hit an error already! (We're still going over the Orders of Operations LOL!!!)

Using BlueJ as the compiler/IDE and all that Jazz.

here is my first problem:
Code:
        System.out.println("46.2 divided by 11 minus 3 plus 24 modulus (17 minus 2 times 3) equals: " + (46.2 / 11) - 3 + 24 % (17 - 2 * 3));


And due to my limited knowledge of Java I have little idea what direction Java reads its info to cause that error.

It seems that I have an operator error and that it was a stupid error.

Surrounding it in brackets all the way like so:

Code:
System.out.println("46.2 divided by 11 minus 3 plus 24 modulus (17 minus 2 times 3) equals: " + [b]([/b](46.2 / 11) - 3 + 24 % (17 - 2 * 3))[b])[/b];



Seems to have fixed the problem. Now, my question is, why did Java need those two brackets there? (rhetorical question, answered on my own)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 11:45 am 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
My take on it is that it is a casting problem (and not necessarily a precedence problem) due to the way you are showing the results. In the first example, you are concatenating a string (your description of what's going on) with your formula, so the compiler is munged up thinking chunks or everything is a string and calculates funky or errors out. If you didn't have the string there, this would probably work fine.

In the second example, you are concatenating a string with the results of your formula, so a string plus a number, and thus it works correctly. The compiler knows due to the brackets that everything there needs to be calculated out prior to concatenating with the string.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:05 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Now, another issue which would be small if I knew what I was doing yet at this point i do Not know what I am doing

Code:
        int pi = 3.14;


I get the following error: Possible loss of precision. What can I do to fix it and why is this error happening ?

As much explanation as possible would be great. I'd google it but i'm too tired and am going to bed now so yeah.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:29 pm 
Sharptooth
Sharptooth
User avatar

Joined: Thu Sep 20, 2007 5:57 pm
Posts: 386
Dwood15 wrote:
Now, another issue which would be small if I knew what I was doing yet at this point i do Not know what I am doing

Code:
        int pi = 3.14;


I get the following error: Possible loss of precision. What can I do to fix it and why is this error happening ?

As much explanation as possible would be great. I'd google it but i'm too tired and am going to bed now so yeah.


Try to do
Code:
const float pi = 3.14;

or
Code:
float pi =3.14;
[/code]
That should get rid of the losing of precision. A float is able to store a digit with a precision of 7 digits while a int is only able to store whole numbers.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 5:25 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Dwood15 wrote:
Now, another issue which would be small if I knew what I was doing yet at this point i do Not know what I am doing

Code:
        int pi = 3.14;


I get the following error: Possible loss of precision. What can I do to fix it and why is this error happening ?

As much explanation as possible would be great. I'd google it but i'm too tired and am going to bed now so yeah.


You are defining a float as an integer. You can't do that without losing everything after the decimel.

Look up 'int' and 'float' and learn the difference between the two. That will explain the problem you're having.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2008 6:54 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Jipstyle wrote:
You are defining a float as an integer. You can't do that without losing everything after the decimel.

Look up 'int' and 'float' and learn the difference between the two. That will explain the problem you're having.


It seems you are correct, however for all intents and purposes,
Code:
double pi = 3.14
was the correct answer to my problem even though I will look for more explanation for floats in Java.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2008 6:34 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Seems?? :P

A double is a type of floating point (float) with double the precision.

Read up on variable types .. the specifics change between programming languages, but the basic catdgotiesx tend to be the same [*].

[*]: perl is an exception. Avoid perl for now.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:30 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Jipstyle wrote:
Seems?? :P

A double is a type of floating point (float) with double the precision.

Read up on variable types .. the specifics change between programming languages, but the basic catdgotiesx tend to be the same [*].

[*]: perl is an exception. Avoid perl for now.


I'm still thinking like a human, :lol: therefore the first 10 or so explanations from google are fail.

Need something humans can understand.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:33 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Ask, and you shall receive! :)

Integer (int): numbers without a decimel. 1, 3245, -123456123412 are all integers. 2.3 is not an integer.

Float: numbers with a decimel. 2.4325, 0.0123456, 2.0 are all floats.

Double: a float that takes up twice as much memory space (and can thus hold a 'bigger' number. In this case, 0.123412352465345 is bigger than 100.1 because it requires more space to store).

Char: a single character. 1, f, % are all chars.

Boolean: can be one of two values: true or false. Sometimes represented as 1 or 0.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:40 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Interesting, I thought floats were bigger. Floats were never really talked about in my intro classes, just doubles.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:48 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Jipstyle wrote:
Ask, and you shall receive! :)

Float: numbers with a decimel. 2.4325, 0.0123456, 2.0 are all floats.

Double: a float that takes up twice as much memory space (and can thus hold a 'bigger' number. In this case, 0.123412352465345 is bigger than 100.1 because it requires more space to store).

Char: a single character. 1, f, % are all chars.


Quick Question: Does this mean simply declaring something as a double automatically takes up more memory? Or that Java is prepared (ready) for a massive number (memory-wise)?

Then, if the latter why not simply use Doubles the entire time?

(Mostly curiosity here more than anything)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:52 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Yep, it does take up twice the space .. but no, it is not something that should concern you. Java will take care of it and your PC has more than enough space that you could easily declare everything to be a double.

For 32-bit computing, a float requires ('takes up') 32-bits of space. A double takes up 64 bits (twice as much .. double .. ;) ).

For 64 bit computing, these numbers are doubled.

Yes, doubled. You'll find that almost everything in computer science gets doubled or halved A LOT. I'll leave it to you to figure out why. ;)

Why don't we declare nothing but doubles? There are a couple of reasons.

One: just because we have the space does not mean that we need to use it. It is sloppy to assign resources 'just because we can'.

Two: there are many applications where this small amount of space can make a difference. Not every codes for PCs ... there are also phones, PDAs, etc. ... and these require that programmers are more conscious of their space decisions.

Three: a double takes up two 'natural spaces' in your PC. A 'natural space' (commonly called a 'word') is 32-bits for a 32-bit computer and 64-bits for a 64-bit computer (etc). It is the amount of data that fits into a single 'slot' in memory. There are many advantages to keeping your data in line with the size of the 'slots' in your PC, but they are beyond the scope of this discussion. Let's just be Martha Stewart and say that using floats where possible is A Good Thing. ;)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 7:57 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Jipstyle wrote:
Yep, it does take up twice the space .. but no, it is not something that should concern you.

For 32-bit computing, a float requires ('takes up') 32-bits of space. A double takes up 64 bits (twice as much .. double .. ;) ).

For 64 bit computing, these numbers are doubled.

Yes, doubled. You'll find that almost everything in computer science gets doubled or halved A LOT. I'll leave it to you to figure out why. ;)



Would thinking about it in Dimensions help?

Like 64 bit is (what I consider) the 4th dimension of 16 bit?

[i'll figure out the real-world application later :D]

You mean using them where necessary is a good thing?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2008 8:03 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
*shakes head* You're making this more complicated .. 4th dimension of what now? :lol:

It is good practice to only use doubles when you need the extra precision. Otherwise, use a float. If you don't expect the number to ever require a decimel, use an integer.

For instance, if you need to store the number of students in a class, use an integer. You will never have 2.4 people in a class (we round up with amputees).


Top
  Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 2:46 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
^ I don't know.

Okay after having let the thread die... I have progressed a bit, however before I go any further in my posting, I need confirmation on my human explanation for the behavior of the scanner class.

Let's say a person has

Code:
        Scanner two = new Scanner(System.in);

        System.out.print("Please enter a decimal value:  ");
        double decimalValue = two.nextDouble();
        System.out.println("You entered: " + decimalValue);
        System.out.println();


Two automatically becomes a variable which (in this case, at least [or in every case?]) the scanner object [two] is a medium for asking the user for input for another variable.

nextDouble says that what the user inputs WILL be a double [else error if it doesn't match]

The next user input will be the next time decimalValue is asked for.

Is there anything that I missed or got wrong?

Edit:

Code:
        System.out.print("Enter three words separated by spaces: ");
        String word1 = in.next();
        String word2 = in.next();
        String word3 = in.next();


WHY does that work? [truncated nonessential code for understanding btw]


Top
  Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 4:27 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Answering your last question, the next() method of the scanner class reads up to the first whitespace character. This can be a space, a tab, an enter, etc. so if you input: foo bar code
"foo" will be word1, "bar" will be word2, and "code" will be word3.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 1:49 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Thanks Quer.

Okay guys, can you explain to me how to effectively use parsing?

Say... a time calculator that converts hours to minutes. (not part of the assignment, btw. I just want to get help in understanding it so I can later on down the road.


Code:
int timeHrs = parseInt(timeMin);


There's no method declared for that. What would be some relevant methods?

(note I am parsing user input [duh])


Top
  Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 3:51 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
The method parseInt(int) that you need is in the class Integer. So...
Code:
String s42 = "42";
int i42 = Integer.parseInt(s42);

note that this method only handles "pure" number strings, not something like "42 is an integer" or "forty-two".
...or maybe your teacher wants you to write your own.Which is kinda pointless except to see how it works.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 8:03 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Quertior wrote:
The method parseInt(int) that you need is in the class Integer. So...
Code:
String s42 = "42";
int i42 = Integer.parseInt(s42);

note that this method only handles "pure" number strings, not something like "42 is an integer" or "forty-two".
...or maybe your teacher wants you to write your own.Which is kinda pointless except to see how it works.



Agreed. In the assignment I decided to stick with the predefined integer for method. (we'll see what the teach has to say about that)

My problem is understanding what's going on in the background. What is the code saying?

It's like Knowing how to use it but I need to understand WHAT is going on.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2009 8:21 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Dwood15 wrote:
It's like Knowing how to use it but I need to understand WHAT is going on.
That is an entire class by itself... lol, and then there is an entire area of study on that.

http://www.amazon.com/Introduction-Form ... 0763737984

Good book. I actually liked my Automata class... then again, I didn't have any tests... :lol:


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 58 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group