MHB Using constants in expressions.

AI Thread Summary
The discussion revolves around a Java code snippet for a shipping cost calculator, highlighting several coding errors and issues. Key points include the incorrect usage of the variable "CENTS_PER_POUND," which is declared but not initialized, leading to potential compiler errors. Additionally, there is a typo with "shipCostCost," which should be "shipCostCents," indicating a lack of attention to detail. Participants emphasize the importance of clearly describing coding problems and suggest that understanding how to articulate issues is crucial for learning programming. The conversation also touches on the moderation of forum posts, specifically addressing the term "necroposting," which refers to responding to old threads, often leading to moderation due to spam concerns. Overall, the thread underscores the significance of proper coding practices and effective communication in programming discussions.
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;
   }
}
 
Technology 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 Langauge 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.
 
  • #10
Ahhhhh...I see...merci beaucoup Marcel :)

"necroposted" means "possible spammer"?
 
  • #11
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. :)
 
  • #12
"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.
 
Back
Top