Using constants in expressions.

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 4K views
dcs1953
Messages
3
Reaction score
0
Code:
import java.util.Scanner;

public class ShippingCalculator {
   public static void main (String [] args) {
      int shipWeightPounds = 10;
      int shipCostCents = 0;
      final int FLAT_FEE_CENTS = 75;

     shipWeightPound = shipCostCost / shipWeightPound;
     final int CENTS_PER_POUND;

      System.out.print("Weight(lb): " + shipWeightPounds);
      System.out.print(", Flat fee(cents): " + FLAT_FEE_CENTS);
      System.out.print(", Cents per pound: " + CENTS_PER_POUND);
      System.out.println(", Shipping cost(cents): " + shipCostCents);

      return;
   }
}
 
Physics news on Phys.org
So far you have copy/pasted blocks of code (without wrapping them in the appropriate tags), and you haven't described what the problem is, what you've tried and where you're stuck. We want to help, but you have to show a little effort here. :D
 
You have a line that writes out "CENTS_PER_POUND" which is defined as an "int" but never given a value. What happens will depend upon the compiler. Most would, I think, give an error message. Some will print "0" but some will print whatever random number happens to be in the memory location assigned to "CENTS_PER_POUND".
 
HallsofIvy said:
You have a line that writes out "CENTS_PER_POUND" which is defined as an "int" but never given a value.
Yes, Java language Specification (section 4.12.5) says that a local variable (i.e., a variable declared inside a method) must be explicitly given a value before it is used, by either initialization or assignment. My compiler signals an error.

But I agree that learning to describe a problem is arguably more important than learning any Java.
 
Another thing: in the first line of actual computation, you have "shipCostCost" but there is no such variable. I suspect you intended "shipCostCents". That will give an error message but the error message should be pretty clear. Again, what was your purpose in posting this? Do you have a question about it?
 
dcs1953 said:
Code:
import java.util.Scanner;

public class ShippingCalculator {
   public static void main (String [] args) {
      int shipWeightPounds = 10;
      int shipCostCents = 0;
      final int FLAT_FEE_CENTS = 75;

     shipWeightPound = shipCostCost / shipWeightPound;
     final int CENTS_PER_POUND;

      System.out.print("Weight(lb): " + shipWeightPounds);
      System.out.print(", Flat fee(cents): " + FLAT_FEE_CENTS);
      System.out.print(", Cents per pound: " + CENTS_PER_POUND);
      System.out.println(", Shipping cost(cents): " + shipCostCents);

      return;
   }
}
Sorry...but the whole darn thing is 99_PERCENT_RIDICULOUS !
 
My compiler is also slightly stuck, probably there is an error with the variable
 
HEY moderators, just got this by email:

Arthur Gerrard has just replied to a thread you have subscribed to entitled - Using constants in expressions. - in the Computer Science forum of Math Help Boards | Free Math Help.
This thread is located at:
https://mathhelpboards.com/computer-science-58/using-constants-expressions-21125-new.html
Here is the message that has just been posted:
***************
My compiler is also slightly stuck, probably there is an error with the variable
***************

BUT I see no such post by Sir Gerrard!
 
Wilmer said:
HEY moderators, just got this by email:

Arthur Gerrard has just replied to a thread you have subscribed to entitled - Using constants in expressions. - in the Computer Science forum of Math Help Boards | Free Math Help.
This thread is located at:
https://mathhelpboards.com/computer-science-58/using-constants-expressions-21125-new.html
Here is the message that has just been posted:
***************
My compiler is also slightly stuck, probably there is an error with the variable
***************

BUT I see no such post by Sir Gerrard!

The post was put into the moderation queue because a new user "necroposted." I have just approved the post.
 
Ahhhhh...I see...merci beaucoup Marcel :)

"necroposted" means "possible spammer"?
 
Wilmer said:
Ahhhhh...I see...merci beaucoup Marcel :)

"necroposted" means "possible spammer"?

Yes, that is one of the behaviors of spammers. Unfortunately, this gets innocent posts like the one above moderated as well. But, as with everything the toxic minority of tomfoolers makes things difficult for everyone. :)
 
"Necroposting" specifically refers to posting a reply to a "dead" thread. In this case Arthur Gerard was, on May 8, 2018, responding on a thread where there had been no response since Oct. 18, 2017, 8 months earlier.