Using constants in expressions.

In summary: This is not illegal but is impolite. It is very difficult for anyone to remember what that thread was about and what it was that led to any particular response. When I wrote a response to the thread I started out by saying "I don't remember what this thread is about."
  • #1
dcs1953
3
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
  • #2
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
 
  • #3
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".
 
  • #4
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.
 
  • #5
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?
 
  • #6
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 !
 
  • #7
My compiler is also slightly stuck, probably there is an error with the variable
 
  • #8
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!
 
  • #9
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.
 

Related to Using constants in expressions.

Q: What is a constant in an expression?

A constant in an expression is a fixed value that does not change throughout the computation. It is used to represent a specific value, such as a number or string, and is written in the code without any calculations or variables.

Q: Why are constants important in expressions?

Constants are important in expressions because they provide a way to store and manipulate values that do not change. This allows for more efficient and accurate calculations, as well as easier maintenance of code.

Q: How are constants declared in expressions?

Constants are typically declared using the keyword "const" followed by the name of the constant and its value. For example, const PI = 3.14; In some programming languages, constants may also be declared using the keyword "let" or "var" with a specific syntax.

Q: Can constants be reassigned in expressions?

No, constants cannot be reassigned or changed during the execution of the program. This is the main difference between constants and variables, which can be reassigned multiple times. Attempting to reassign a constant will result in an error.

Q: What are some common examples of constants used in expressions?

Some common examples of constants used in expressions include mathematical constants such as pi (3.14), the speed of light (299,792,458 m/s), and the gravitational constant (9.8 m/s²). Other examples include constants used in programming, such as the maximum value for an integer or the number of seconds in a minute.

Similar threads

  • Programming and Computer Science
Replies
3
Views
910
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
8
Views
9K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
12K
Back
Top