Syntax Problems
Author |
Message |
Zren

|
Posted: Thu Dec 22, 2011 7:03 pm Post subject: RE:Syntax Problems |
|
|
A) Yes that would work. The top of the fraction doesn't necessarily need to be a real number too, just one side of the fraction.
B) If you wanted to use an integer variable for the bottom, yes you'd need to cast it as a double first if you wanted decimal values. However, understand that what you just wrote is casting 5 as a double, then dividing by the integer 9 (not integer division as top is double). It's not casting the entire fraction. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Aange10

|
Posted: Thu Dec 22, 2011 7:13 pm Post subject: RE:Syntax Problems |
|
|
I see, thank you for that; (((double)5)/((double)9)) * (fahrenheit - 32.0) now works perfectly. How would I make it, when I output it, only show two decimal places? |
|
|
|
|
 |
Zren

|
|
|
|
 |
Aange10

|
Posted: Thu Dec 22, 2011 9:39 pm Post subject: RE:Syntax Problems |
|
|
Thanks for that. The string format is pretty cool, and I can use it just like Str.Lower(). (I believe it also has a function like that).
Anyways, to another problem! I despise this, because the problems are so stupid. Its less embarrassing when it's the algorithm messing things up, not the ignorance.
So this problem makes absolutely no sense to me. There are no errors, and I've traced my algorithm with comments. Its super simple, it won't output to the screen! But I don't see why, everything is nested correctly.
Once again I have two classes, Main and Sort. Main is easy and looks like this:
Java: |
public class Main {
public static void main (String [] args ){
Sort foo = new Sort ();
foo. Sort10();
}
}
|
Just making an instance of Sort, and calling one of its methods.
This is the sort class. I have the algorithm traced, though my problem isn't whats being computed. It's the fact that it won't even print to the screen! (Makes me think I have some brackets messed up but I don't):
Java: |
import java.util.Scanner;
public class Sort {
void Sort10 (){
int num [] = new int[9]; // Make an int array num 0 .. 9
int temp; // temporary variable
String suffix; // suff string
Scanner input = new Scanner (System. in); // to get input
for(int i= 0; i== 9; i++ ) { // for 0 .. 9
// An if to get the correct suffix
if (i + 1== 1){
suffix = "st";
} else if (i + 1 == 2) {
suffix = "nd";
} else if (i + 1 == 3) {
suffix = "rd";
} else {
suffix = "th";
}
// print to the console
System. out. println ((i + 1) + suffix + " input, please.");
// get input
num [i ] = input. nextInt();
}
// Bubble sort through input
for (int i= 0; i == num. length - 1; i++ ) { // 0 .. 8
for (int e = num. length - 1; e == i; e = e - 1){ // 8 .. i by -1
if (num [e ] > num [e + 1]){ // if num[e] is greater then num[e + 1]
temp = num [e + 1]; // store num [e + 1]
num [e + 1] = num [e ]; // move num [e + 1] to num [e]
num [e ] = temp; // give nume [e] num [e + 1]'s old value
}
}
}
// Output our numbers
for (int i = 0; i == num. length; i ++ ){
System. out. println (num [i ]);
}
}
}
|
Also, on a side note, I imported Scan. Is that only going to be valid for the objects in the class I uploaded it to, or will it be valid for my entire program? |
|
|
|
|
 |
Tony

|
Posted: Thu Dec 22, 2011 9:59 pm Post subject: RE:Syntax Problems |
|
|
code: |
int num[] = new int[9]; // Make an int array num 0 .. 9
|
that doesn't look right, does this have to be on the heap?
Otherwise try placing some debug print statements in, to see how far the program actually gets.
re: imports -- they are available to this class. JLS says so: http://java.sun.com/docs/books/jls/third_edition/html/packages.html
Quote:
The scope of a type imported by a single-type-import declaration (?7.5.1) or a type-import-on-demand declaration (?7.5.2) is all the class and interface type declarations (?7.6) in the compilation unit in which the import declaration appears.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Aange10

|
Posted: Thu Dec 22, 2011 10:05 pm Post subject: RE:Syntax Problems |
|
|
does this have to be on the heap" What do you mean? And I think the syntax might be int [] num = new int[9]; ... but neither give me a syntax error. And with that change, it doesn't go past the for. whats wrong with the for...? |
|
|
|
|
 |
DemonWasp
|
Posted: Thu Dec 22, 2011 10:05 pm Post subject: RE:Syntax Problems |
|
|
You have at least two problems:
- your array isn't 0-9, it's 0-8 (9 items)
- your for loops never execute because their "continue" conditions (middle) are wrong
To make what Tony said more apparent: imports only apply to the contents of that file. They affect anything within the file, but not the rest of your program.
@Tony: are you forgetting your Java? Java uses new for everything, and everything works like it's on the heap, even when it doesn't end up getting allocated there. |
|
|
|
|
 |
Aange10

|
Posted: Thu Dec 22, 2011 10:11 pm Post subject: Re: RE:Syntax Problems |
|
|
DemonWasp @ 22/12/2011, 9:05 pm wrote: You have at least two problems:
- your array isn't 0-9, it's 0-8 (9 items)
- your for loops never execute because their "continue" conditions (middle) are wrong
Ahh thanks so much.
A: I didn't know that [number] for the array was the item, i thought it was the upper bound.
B: Thanks again, i thought the middle was stopWhenThisIsTrue not continueUntillThisIsFalse
Thanks, both of you. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Thu Dec 22, 2011 10:23 pm Post subject: Re: RE:Syntax Problems |
|
|
DemonWasp @ Thu Dec 22, 2011 10:05 pm wrote: @Tony: are you forgetting your Java? Java uses new for everything, and everything works like it's on the heap, even when it doesn't end up getting allocated there.
Hah, you're right I've been writing nothing but C and C++ for the past 4 months. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Aange10

|
Posted: Thu Dec 22, 2011 11:08 pm Post subject: Re: RE:Syntax Problems |
|
|
Quote:
Hah, you're right  I've been writing nothing but C and C++ for the past 4 months.
Aren't you a ruby dev? |
|
|
|
|
 |
Tony

|
Posted: Thu Dec 22, 2011 11:32 pm Post subject: RE:Syntax Problems |
|
|
Ruby is typically my language of choice, but there isn't much say in school assignments  |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Aange10

|
Posted: Fri Dec 23, 2011 1:23 am Post subject: RE:Syntax Problems |
|
|
A bit OT, but java is really amazing. I can't wait until I learn all the syntax (well, enough).
It's very clean. Everything being so organized by brackets, being stifled into classes is nice. If I want 80 dogs, I don't have to worry about their names getting jumbled, or making sure Dog 70 doesn't have a number in his name; they do it themselves. The classes can take care of them selves. It's a lot different than micromanaging every bit of information you use.
It's amazing you can make a method with parameters to define your instances for you, and if you're using eclipse, when you type up the method it'll lay out the syntax of the arguments for you. It's like a one line initialize, that can be bullet proofed in the method (if you like, and whatever else), and the layout is given to you, so you don't forget a variable. Don't want to assign one? Simple use an if param != null.
Java is really neat in the way it works. I'm eager to experience the inheritance aspect.
So to put this a tad bit on topic, If I make a class Animal with a name and age, and a breathe method - and I decide to make another class Dog, how do I make it inherit from Animal? Because I'd like to give all Dogs the bark method, and favorite toy instance as well(: |
|
|
|
|
 |
Tony

|
|
|
|
 |
Aange10

|
Posted: Fri Dec 23, 2011 1:30 am Post subject: RE:Syntax Problems |
|
|
extends! Ahh, I see. Thanks. |
|
|
|
|
 |
Aange10

|
Posted: Fri Dec 23, 2011 11:13 pm Post subject: RE:Syntax Problems |
|
|
And onto another question:
Does java have a way to detect if a variable has a value? For instance I'd like to make my method look like this:
Java: |
public void applyMonthlyInterest (){
// savingsBalance is an instance of the class holding this method, and it is a double.
if (savingsBalance != null){
savingsBalance = ((savingsBalance) * (annualInterestRate/12.0));
} else {
// error
}
}
|
However this is obviously is an error because a double can't have a value 'null'. Is there a way I can make sure that the variable has a value? |
|
|
|
|
 |
|
|