Using constants in expressions.

  • Context: MHB 
  • Thread starter Thread starter dcs1953
  • Start date Start date
  • Tags Tags
    Constants Expressions
Click For Summary

Discussion Overview

The discussion revolves around issues in a Java program related to the use of constants and variable declarations in a shipping cost calculator. Participants explore coding errors, compiler behavior, and the importance of articulating programming problems effectively.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant points out that the variable "CENTS_PER_POUND" is declared but not initialized, which could lead to compiler errors or undefined behavior.
  • Another participant emphasizes that local variables in Java must be initialized before use, referencing the Java language specification.
  • A different participant identifies a typographical error in the code where "shipCostCost" is likely meant to be "shipCostCents," suggesting this will produce a clear error message.
  • One participant expresses frustration with the overall state of the code, describing it as "99_PERCENT_RIDICULOUS."
  • There is a mention of a post by a user that was moderated due to "necroposting," which led to a discussion about the implications of this term and its association with spam.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple coding errors present in the provided Java code, but there is no consensus on the best way to address the issues or the importance of articulating problems in programming.

Contextual Notes

There are unresolved issues regarding the initialization of variables and the specific behavior of the compiler in response to the code errors. The discussion also touches on the moderation of posts and the implications of necroposting.

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 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.
 
  • #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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
8K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
3K
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 2 ·
Replies
2
Views
6K