Error resolving variables and left-side side assignments and figuring ++ tokens - java

I'm starting to get blind looking at my code and my brain is about to overheat. I'm new when it comes to programming.
public class RecyclingSystem {
public static void main(String[] args) {
System.out.println("Please put in a valid bottle");
Scanner sc = new Scanner(System.in);
while ( sc.nextInt() != -1) {
if (sc.nextInt(char a) = getaBottle);
int bottleAcount++;
} else if { (sc.nextInt(char b) = getbBottle);
int bottleBcount++;
} else if { (sc.nextInt(char c) = getcBottle);
int bottleCcount++;
} else { throw new EmptyStackException();
System.out.println("Bottle not recognized");
}
System.out.println("The total number of bottles is " + totalBottlecount);
System.out.println();
System.out.println("The total amount returned is " + sumOfBottles );
}
sc.close();
}
}}
public class Bottle {
private static final double A_BOTTLE = 1.0;
/**
* #return the aBottle
*/
public static double getaBottle() {
return A_BOTTLE;
}
/**
* #return the bBottle
*/
public static double getbBottle() {
return B_BOTTLE;
}
/**
* #return the cBottle
*/
public static double getcBottle() {
return C_BOTTLE;
}
private static final double B_BOTTLE = 1.5;
private static final double C_BOTTLE = 3.0;
}
public class EmptyStackException extends Exception {
}
public class bottleCount {
int bottleAcount = 0;
int bottleBcount = 0;
int bottleCcount = 0;
int totalBottleCount = bottleAcount + bottleBcount + bottleCcount;
}
I have seperate classes for the getbottle, totalBottlecount and bottlecount variables.
I want to make a user-input based recycling system simulator, if that makes any sense, with 3 different types of bottles, which are assigned different values, a total bottle count and the sum of the values of the 3 bottle types combined.
I get several compiler errors and I have spend HOURS trying to resolve them all, but every time I do, new errors occur and now I get a "coders-block".
I get asked to delete the ++ tokens, the compiler cannot resolve my variables and syntax errors. I would really appreciate some insight, since I'm only ~3weeks into java programming.
UPDATED: Compiler errors exact copy pasta
Multiple markers at this line
- Syntax error, insert ")" to complete Expression
- Duplicate method nextInt(char) in type RecyclingSystem
- Syntax error, insert "}" to complete Block
- Syntax error, insert "AssignmentOperator Expression" to complete Assignment
- Return type for the method is missing
- Syntax error on tokens, delete these tokens
- The left-hand side of an assignment must be a variable
- Syntax error, insert "AssignmentOperator Expression" to complete Expression
- The left-hand side of an assignment must be a variable
- Syntax error, insert ";" to complete BlockStatements
- Syntax error on tokens, delete these tokens
- Syntax error on token ")", { expected after this token
- Syntax error, insert ";" to complete Statement

int bottleAcount++;
In java you need to declare the local variable like
type name = intial value;
then do any operation on that like increament or decrement.
In youe case declar the variable before while loop with zero as intial value like
int bottleAcount = 0;
then inside while increament it by 1, like bottleAcount++;
or
bottleAcount += 1;

So... If this is all the code there are many problems and what can I recommend in the beginning - go back to some basic Java programming course.
Let's look at one of the first lines:
if (sc.nextInt(char a) = getaBottle);
Firstly, it's a condition, and you are assigning the value of a getaBottle to the sc.nextInt(char a).
Secondly, nextInt(char a) looks like method declaring, not like a method call.
Thirdly, getaBottle is never declared before
Fourthly, you have a getaBottle() method in a Bottle class, which you probably want to use instead of getaBottle which is (should) be a variable
...etc., etc.
This code is not even valid Java code. It's hard to help you somehow in that problem, you just need to learn a bit more, which I encourage you to do.
Good luck and in case of any specific question - come and ask!

else if { (sc.nextInt(char b) = getbBottle);
int bottleBcount++;
}
Syntax is totally wrong for if.Also condition is checked using == not = It should be :
else if (sc.nextInt(char b) == getbBottle);
int bottleBcount++;
Also you cant do int bottleBcount++. Its meaningless. Since you already declared bottleBcount in another class, you have to access it using the object of that class. Before that change the declaration in the class from int bottleAcount = 0; to public int bottleAcount = 0; . Do this for all bottles.
Now you can use it as :
public static void main(String[] args) {
System.out.println("Please put in a valid bottle");
bottleCount counter = new bottleCount();
Scanner sc = new Scanner(System.in);
while ( sc.nextInt() != -1) {
if (sc.nextInt(char a) == getaBottle);
counter.bottleAcount++;
} else if (sc.nextInt(char b) == getbBottle);
counter.bottleBcount++;
else if (sc.nextInt(char c) == getcBottle);
counter.bottleCcount++;
else { throw new EmptyStackException();
System.out.println("Bottle not recognized");
}
System.out.println("The total number of bottles is " + totalBottlecount);
System.out.println();
System.out.println("The total amount returned is " + sumOfBottles );
}
sc.close();
}
Also the statement int totalBottleCount = bottleAcount + bottleBcount + bottleCcount; doesnt make sense. It won't work as you wanted it to. You need to write a function to do this addition and then call it. Or if you want this to happen just once ( which I doubt) , you can put it in a constructor.
I suggest you brush up on class and variable declaration concepts before proceeding

You just have problem in this:
else {
throw new EmptyStackException();
System.out.println("Bottle not recognized");
}
Check the proper syntax and error will be resolved.

Related

For loop and if statement within a method

I am new to using java and am having some issues in my java class right now and will be needing help with my specific code. I try to look at others questions on here all the time but it's never exactly what I need. Here are my directions:
Create a Java file called CompoundInterestYourLastName. Write a method called computeBalance() that computes the balance of a bank account with a given initial balance and interest rate, after a given number of years. Assume interest is compounded yearly.
Use a loop to control the iterations through the years in your method.
Your method should return a double value.
In your main method, run the following tests to verify your method is working correctly.
System.out.printf("Your total is $%.2f", computeBalance(1000, .045, 3));
// should return $1141.17
I am using eclipse and my only current error is in the comments. I also want some general tips and let me know if my logic is wrong. It probably is. :D
Here is what I have currently although I have been trying different things:
import java.util.Scanner;
import java.lang.Math;
public class CompoundInterestTidwell {
public static void main(String[] args) {
double compInt = computeBalance(1000, 0.045, 3);
System.out.printf("Your new balance is $%.2f", compInt);
}
// Getting arror for line of code below.
// Error: This method must return a result of type double
public static double computeBalance(int P, double r, int t) {
// Formula for compounding interest
// A = P(1+(r/n))^(n(t))
// The examples to check my math had rate already divided by 100 so I left out r/n.
for(int c = 0; c <= t; c++ ) {
// deleted 'n' from equation because it need to equal 1 anyways.
double compInt = Math.pow(P*(1+r), t);
if (c < t) {
c++;
return compInt;
}
}
}
}
Thanks.
Your function computeBalance doesn't guarantee to return a value, because the only return statement is in an if clause, within a loop (making it two conditions deep).
This is a thing the compiler is warning you about. Basically it scans your code and makes sure that a function declared as double will actually return a valid value of type double and so on.
If you add a return statement at the end of the body in the function (or throw an error) it should compile.
I am not exactly sure what your function does in technical terms, but I've rewritten it so it should return the same value, but should now actually compile.
public static double computeBalance(int P, double r, int t) {
// Formula for compounding interest
// A = P(1+(r/n))^(n(t))
// The examples to check my math had rate already divided by 100 so I left out r/n.
double compInt = 0; // Declare compInt outside the loop.
for(int c = 0; c <= t; c++ ) {
// deleted 'n' from equation because it need to equal 1 anyways.
compInt = Math.pow(P*(1+r), t);
if (c < t) {
c++;
break; // Break instead of return, will immediately
// go to the return statement outside the loop.
}
}
return compInt; // Moved the return statement to outside the loop so
// the function always will return a double.
}

UVa Online Judge 100th (3n+1) uDebug shows everything as correct but wrong answer

This is my first UVa submission so I had a few problems in the way. The biggest hurdle that took my time so far was probably getting all the formats correctly (I know, shouldn't have been too hard but I kept getting runtime error without knowing what that actually meant in this context). I did finally get past that runtime error, but I still get "Wrong answer."
Listed below are the things I've done for this problem. I've been working on this for the last few hours, and I honestly thought about just dropping it altogether, but this will bother me so much, so this is my last hope.
Things I've done:
considered int overflow so changed to long at applicable places
got the whole list (1-1000000) in the beginning through memorization for computation time
submitted to uDebug. Critical input and Random input both show matching output.
submitted to to UVa online judge and got "Wrong Answer" with 0.13~0.15 runtime.
Things I'm not too sure about:
I think I read that UVa doesn't want its classes to be public. So I left mine as class Main instead of the usual public class Main. Someone from another place mentioned that it should be the latter. Not sure which one UVa online judge likes.
input. I used BufferedReader(new InputStreaReader (System.in)) for this. Also not sure if UVa online judge likes this.
I thought my algorithm was correct but because of "Wrong answer," I'm not so sure. If my code is hard to read, I'll try to describe what I did after the code.
Here is my code:
class Main {
public static int mainMethod(long i, int c, List<Integer> l) {
if (i==1)
return ++c;
else if (i%2==0) {
if (i<1000000&&l.get((int)i)!=null)
return l.get((int)i)+c;
else {
c++;
return mainMethod(i/2, c, l);
}
}
else {
if (i<1000000&&l.get((int)i)!=null)
return l.get((int)i)+c;
else {
c++;
return mainMethod(i*3+1, c, l);
}
}
}
public static int countMax(int x, int y, List<Integer> l) {
int max=0;
if (x>y) {
int temp = x;
x= y;
y = temp;
}
for (int i=x; i<=y; i++) {
if (l.get(i)>max)
max = l.get(i);
}
return max;
}
public static void main(String[] args) {
List<Integer> fixed = Arrays.asList(new Integer[1000000]);
for (long i=1; i<1000000; i++) {
fixed.set((int)i, mainMethod(i,0,fixed));
}
String s;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((s = br.readLine())!=null) {
int x = -1;
int y = -1;
for (String split : s.split("\\s+")) {
if (!split.equals("\\s+") && x==-1) {
x = Integer.parseInt(split);
} else if (!split.equals("\\s+") && x!=-1) {
y = Integer.parseInt(split);
}
}
if (x!=-1&&y!=-1)
System.out.println(Integer.toString(x) + " " + Integer.toString(y) + " " + Integer.toString(countMax(x,y,fixed)));
}
} catch (IOException e) {
} catch (NumberFormatException e) {
}
}
}
I apologize for generic names for methods and variables. mainMethod deals with memorization and creating the initial list. countMax deals with the input from the problem (15 20) and finding the max length using the list. The for loop within the main method deals with potential empty lines and too many spaces.
So my (if not so obvious) question is, what is wrong with my code? Again, this worked perfectly fine on uDebug's Random Input and Critical Input. For some reason, however, UVa online judge says that it's wrong. I'm just clueless as to where it is. I'm a student so I'm still learning. Thank you!
Haven't spotted your error yet, but a few things that may make it easier to spot.
First off:
int goes to 2^31, so declaring i in mainMethod to be long is unnecessary. It also states in the problem specification that no operation will overflow an int, doesn't it? Getting rid of the extraneous longs (and (int) casts) would make it easier to comprehend.
Second:
It's probably clearer to make your recursive call with c + 1 than ++c or doing c++ before it. Those have side effects, and it makes it harder to follow what you're doing (because if you're incrementing c, there must be a reason, right?) What you're writing is technically correct, but it's unidiomatic enough that it distracts.
Third:
So, am I missing something, or are you never actually setting any of the values in the List in your memoization function? If I'm not blind (which is a possibility) that would certainly keep it from passing as-is. Wait, no, definitely blind - you're doing it in the loop that calls it. With this sort of function, I'd expect it to mutate the List in the function. When you call it for i=1, you're computing i=4 (3 * 1 + 1) - you may as well save it.

Error with if expression algorithm

I want to pass in the values of x, y and z, and have them go through the algorithm. But I get an error. I think I'm doing something illegal, but I'm not sure.
The error is with this statement:
if((istrue1)||(istrue2&&istrue3)){
;
}
My full code is:
package com.company;
public class Main {
public static void main(int x, int y, int z) {
boolean istrue1, istrue2, istrue3;
if((istrue1=(x >=1 && x<=31)) || (istrue2=(y>=1 && y<=31)) || (istrue3=(z >= 1 && z<=31)));{
if((istrue1)||(istrue2&&istrue3)){
;
}
}
}
}
There are (at least) two problems in the code (but I suspect only the second is part of your issue here.
The first problem is that your outer if statement has a ';' at the end. So although it looks from the indenting that you have nested if statements, you don't really.
The second problem is a bit more subtle, and it's to do with the if statement short-circuiting the evaluation of its condition.
You have
if
(
a = first_condition ||
b = second_condition ||
c = third_condition
)
{
...do stuff
}
This is legal syntax, but if first_condition is true, then the compiler knows that the whole if condition is true, so it doesn't bother evaluating the second two clauses. That means, if first_condition is true, then neither 'b' nor 'c' will be assigned values.
My advice would be to redo the code as
boolean a = first_condition
boolean b = second_condition
boolean c = third_condition
if (a || b || c)
{
//do stuff
}
There is so much wrong with your approach it is pretty much unsalvageable:
And to explain how and why all the problems in detail would be an entire class and is out of scope for StackOverflow because it would be too broad a subject.
That said, you did make an attempt so here is how to actually do part of what you are trying to do. Hopefully this will clarify how to work with boolean logic.
/* This removes the duplication of logic and remediates
the *line noise* looking code that duplication introduced.
*/
public static boolean inRangeInclusive(final int low, final int value, final int hi)
{
return low <= value && value <= hi;
}
public static void main(final String[] args)
{
final int x = Integer.parseInt(args[0]);
final int y = Integer.parseInt(args[1]);
final int z = Integer.parseInt(args[2]);
if ( inRangeInclusive(1,x,31) || inRangeInclusive(1,y,31) || inRangeInclusive(1,z,31)) ;
{
/* whatever you want to happen if any of those things match goes here */
}
}
Take the time to learn how to make all method args final and all local references final it will save your more time than you can ever imagine!
If write something and it looks like line noise or the cat walked across the keyboard then it is probably wrong. Even if it appears to produce the behavior you desire.

Class to count variables design issue

I'm new to OO programing and having a bit of trouble with the design of my program to use the concepts. I have done the tutorials but am still having problem.
I have a recursion that takes a value of items(could be anything in this example, stocks) and figures out what number of them are needed to equal a specific value(in this code 100). This part works but I want to know if a stock's weighting exceeds a threshold. Originally I approached this problem with a method that did a for loop and calculated the entire list of values but this is super inefficient because its doing it on every loop of the recursion. I thought this would be a good time to try to learn classes because I could use a class to maintain state information and just increment the value on each loop and it'll let me know when the threshold is hit.
I think I have the code but I don't fully understand how to design this problem with classes. So far it runs the loop each step of the recursion because I'm initially the class there. Is there a better way to design this? My end goal is to be notified when a weighting is exceeded(which I can somewhat already do) but I want to do in way that uses the least bit of resources(avoiding inefficient/unnecessary for loops)
Code(Here's the entire code I have been using to learn but the problem is with the Counter class and its location within the findVariables method):
import java.util.Arrays;
public class LearningClassCounting {
public static int[] stock_price = new int[]{ 20,5,20};
public static int target = 100;
public static void main(String[] args) {
// takes items from the first list
findVariables(stock_price, 100, new int[] {0,0,0}, 0, 0);
}
public static void findVariables(int[] constants, int sum,
int[] variables, int n, int result) {
Counter Checker = new Counter(stock_price, variables);
if (n == constants.length) {
if (result == sum) {
System.out.println(Arrays.toString(variables));
}
} else if (result <= sum){ //keep going
for (int i = 0; i <= 100; i++) {
variables[n] = i;
Checker.check_total_percent(n, i);
findVariables(constants, sum, variables, n+1, result+constants[n]*i);
}
}
}
}
class Counter {
private int[] stock_price;
private int[] variables;
private int value_so_far;
public Counter(int[] stock_price, int[] variables) {
this.stock_price = stock_price;
this.variables = variables;
for (int location = 0; location < variables.length; location++) {
//System.out.println(variables[location] + " * " + stock_price[location] + " = " + (variables[location] * stock_price[location]) );
value_so_far = value_so_far + (variables[location] * stock_price[location]);
}
//System.out.println("Total value so far is " + value_so_far);
//System.out.println("************");
}
public void check_total_percent(int current_location, int percent) {
// Check to see if weight exceeds threshold
//System.out.println("we are at " + current_location + " and " + percent + " and " + Arrays.toString(variables));
//System.out.println("value is " + stock_price[current_location] * percent);
//formula I think I need to use is:
if (percent == 0) {
return;
}
int current_value = (stock_price[current_location] * percent);
int overall_percent = current_value/(value_so_far + current_value);
if (overall_percent > 50 ) {
System.out.println("item " + current_location + " is over 50%" );
}
}
}
What you're describing sounds like a variant of the famous knapsack problem. There are many approaches to these problems, which are inherently difficult to calculate.
Inherently, one may need to check "all the combinations". The so-called optimization comes from backtracking when a certain selection subset is already too large (e.g., if 10 given stocks are over my sum, no need to explore other combinations). In addition, one can cache certain subsets (e.g., if I know that X Y and Z amount to some value V, I can reuse that value). You'll see a lot of discussion of how to approach these sort of problems and how to design solutions.
That being said, my view is that while algorithmic problems of this sort may be important for learning how to program and structure code and data structures, they're generally a very poor choice for learning object-oriented design and modelling.

How to avoid 'the local variable may not have been initialized'?

/*This is a program that calculates Internet advertising rates based on what features/options you choose.
*
*
*/
import java.util.Scanner;
public class InternetAdvertising
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int numberOfWords;
//I assigned 0 values to both as Eclipse suggested
float textCost = 0;
float linkCost = 0;
float graphicCost;
//<=25 words is a flat fee of $.40 per word plus Base fee of $3.00
final float TEXT_FLAT_FEE = 0.40F;
final float TEXT_BASE_FEE = 3.00F;
//<=35 words is $.40 for the first 25 words and
//an additional $.35 per word up to and including 35 words plus Base fee of $3.00
final float LESS_OR_EQUAL_THAN_THIRTYFIVE = 0.35F;
//Over 35 words is a flat fee of $.32 per word with no base fee
final float MORE_THAN_THIRTYFIVE = 0.32F;
System.out.println("Welcome!");
System.out.print("Enter the number of words in your ad: ");
numberOfWords = in.nextInt();
if (numberOfWords <= 25)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * numberOfWords);
}
else if (numberOfWords <= 35)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * 25) + (numberOfWords - 25) * LESS_OR_EQUAL_THAN_THIRTYFIVE;
}
else if (numberOfWords > 35)
{
textCost = numberOfWords * MORE_THAN_THIRTYFIVE;
}
String addLink, advancePay;
char link, advPay;
final float LINK_FLAT_FEE = 14.95F;
final float THREE_MONTH_ADV_DISCOUNT = 0.10F;
System.out.print("Would you like to add a link (y = yes or n = no)? ");
addLink = in.next();
link = addLink.charAt(0);
link = Character.toLowerCase(link);
if (link == 'y')
{
System.out.print("Would you like to pay 3 months in advance " + "(y = yes or n = no)? ");
advancePay = in.next();
advPay = advancePay.charAt(0);
advPay = Character.toLowerCase(advPay);
switch (advPay)
{
case 'y':
linkCost = (3 * LINK_FLAT_FEE) - (3 * LINK_FLAT_FEE) * THREE_MONTH_ADV_DISCOUNT;
break;
case 'n':
linkCost = LINK_FLAT_FEE;
break;
}
}
else
{
linkCost = 0;
}
String addGraphic;
char graphic;
System.out.print("Would you like to add graphics/pictures” + “(S = Small, M = Medium, L = Large or N = None)? ");
addGraphic = in.next();
graphic = addGraphic.charAt(0);
graphic = Character.toUpperCase(graphic);
graphic = Character.toLowerCase(graphic);
switch (graphic)
{
case 's':
graphicCost = 19.07F;
break;
case 'm':
graphicCost = 24.76F;
break;
case 'l':
graphicCost = 29.33F;
break;
default:
graphicCost = 0;
}
float gst, totalBeforeGst, totalAfterGst;
final float GST_RATE = 0.05F;
totalBeforeGst = textCost + linkCost + graphicCost; //textCost & linkCost would not initialize
gst = totalBeforeGst * GST_RATE;
totalAfterGst = totalBeforeGst + (totalBeforeGst * GST_RATE);
System.out.printf("\t\t%-16s %11s\n", "Category", "Cost");
System.out.printf("\t\t%-16s %11.2f\n", "Text", textCost); //linkCost would not initialize
System.out.printf("\t\t%-16s %11.2f\n", "Link", linkCost); //textCost would not initialize
System.out.printf("\t\t%-16s %11.2f\n", "Graphic", graphicCost);
System.out.printf("\t\t%-16s %11.2f\n", "Total", totalBeforeGst);
System.out.printf("\t\t%-16s %11.2f\n", "GST", gst);
System.out.printf("\t\t%-16s %11.2f\n", "Total with GST", totalAfterGst);
}
}
I'm almost done with this code and Eclipse suggests that I assign 0 values to textCost and linkCost. Is there any other way to go around this problem. If I don't assign 0 values they get an error (The local variable XXX may not have been initialized). Can someone explain to me why this happens even though I have both variables assigned with equations?
Thanks.
EDIT: I did as suggested and declared the variables only when I'm going to need it. I also added some comments.
Three suggestions before I delve any deeper into the code:
Declare variables as late as you can to make it easier to understand the code.
Refactor this giant method - it's unreadably huge at the moment.
Make the constants static final fields. They're not related to any particular call to the method, so they shouldn't be local variables.
Now as to the actual question, the simplest way is to make sure that every possible flow actually does assign a value or throw an exception. So for textCost, change your code to:
if (numberOfWords <= 25)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * numberOfWords);
}
else if (numberOfWords <= 35)
{
textCost = TEXT_BASE_FEE + (TEXT_FLAT_FEE * 25) + (numberOfWords - 25) *
LESS_OR_EQUAL_THAN_THIRTYFIVE;
}
else // Note - no condition.
{
textCost = numberOfWords * MORE_THAN_THIRTYFIVE;
}
For linkCost, change your switch statement to something like:
switch (advPay)
{
case 'y':
linkCost = (3 * LINK_FLAT_FEE) -
(3 * LINK_FLAT_FEE) * THREE_MONTH_ADV_DISCOUNT;
break;
case 'n':
linkCost = LINK_FLAT_FEE;
break;
default:
throw new Exception("Invalid value specified: " + advPay);
}
Now you may not want to throw an exception here. You might want to loop round again, or something like that. You probably don't want to use just bare Exception - but you should think about the exact exception type you do want to use.
It's not always possible to do this. The rules by the compiler to determine definite assignment are relatively straightforward. In cases where you really can't change the code to make the compiler happy like this, you can just assign a dummy initial value. I'd recommend trying to avoid this wherever possible though. In your first case, the value really would always be assigned - but in the second case you really weren't giving a value when advPay was neither 'y' nor 'n' which could lead to a hard-to-diagnose problem later on. The compiler error helps you spot this sort of problem.
Again though, I strongly suggest you refactor this method. I suspect you'll find it a lot easier to understand why things aren't definitely assigned when there's only about 10 lines of code to reason about in each method, and when each variable is declared just before or at its first use.
EDIT:
Okay, the radically refactored code is below. I'm not going to claim it's the best code in the world, but:
It's more testable. You could easily write unit tests for each part of it. printAllCosts isn't terribly easily testable, but you could have an overload which took a Writer to print to - that would help.
Each bit of calculation is in a logical place. Links and graphics have a small set of possible values - Java enums are a natural fit here. (I'm aware they may well be beyond your current skill level, but it's good to see what will be available.)
I'm not using binary floating point numbers any more, because they're inappropriate for numbers. Instead, I'm using an integer number of cents everywhere and converting to BigDecimal for display purposes. See my article on .NET floating point for more information - it's all relevant to Java really.
The advert itself is now encapsulated in a class. You could add a lot more information here as and when you needed to.
The code is in a package. Admittedly it's all in one file at the moment (which is why only the EntryPoint class is public) but that's just for the sake of Stack Overflow and me not having to open up Eclipse.
There's JavaDoc explaining what's going on - at least for a few methods. (I would probably add some more in real code. I'm running out of time here.)
We validate user input, except for the word count - and we perform that validation in a single routine, which should be reasonably testable. We can then assume that whenever we've asked for input, we've got something valid.
The number of static methods in EntryPoint is slightly alarming. It doesn't feel terribly OO - but I find that's often the way around the entry point to a program. Note that there's nothing to do with fees in there - it's just the user interface, basically.
There's more code here than there was before - but it's (IMO) much more readable and maintainable code.
package advertising;
import java.util.Scanner;
import java.math.BigDecimal;
/** The graphic style of an advert. */
enum Graphic
{
NONE(0),
SMALL(1907),
MEDIUM(2476),
LARGE(2933);
private final int cost;
private Graphic(int cost)
{
this.cost = cost;
}
/** Returns the cost in cents. */
public int getCost()
{
return cost;
}
}
/** The link payment plan for an advert. */
enum LinkPlan
{
NONE(0),
PREPAID(1495), // 1 month
POSTPAID(1495 * 3 - (1495 * 3) / 10); // 10% discount for 3 months up-front
private final int cost;
private LinkPlan(int cost)
{
this.cost = cost;
}
/** Returns the cost in cents. */
public int getCost()
{
return cost;
}
}
class Advertisement
{
private final int wordCount;
private final LinkPlan linkPlan;
private final Graphic graphic;
public Advertisement(int wordCount, LinkPlan linkPlan, Graphic graphic)
{
this.wordCount = wordCount;
this.linkPlan = linkPlan;
this.graphic = graphic;
}
/**
* Returns the fee for the words in the advert, in cents.
*
* For up to 25 words, there's a flat fee of 40c per word and a base fee
* of $3.00.
*
* For 26-35 words inclusive, the fee for the first 25 words is as before,
* but the per-word fee goes down to 35c for words 26-35.
*
* For more than 35 words, there's a flat fee of 32c per word, and no
* base fee.
*/
public int getWordCost()
{
if (wordCount > 35)
{
return 32 * wordCount;
}
// Apply flat fee always, then up to 25 words at 40 cents,
// then the rest at 35 cents.
return 300 + Math.min(wordCount, 25) * 40
+ Math.min(wordCount - 25, 0) * 35;
}
/**
* Displays the costs associated with this advert.
*/
public void printAllCosts()
{
System.out.printf("\t\t%-16s %11s\n", "Category", "Cost");
printCost("Text", getWordCost());
printCost("Link", linkPlan.getCost());
printCost("Graphic", graphic.getCost());
int total = getWordCost() + linkPlan.getCost() + graphic.getCost();
printCost("Total", total);
int gst = total / 20;
printCost("GST", gst);
printCost("Total with GST", total + gst);
}
private void printCost(String category, int cents)
{
BigDecimal dollars = new BigDecimal(cents).scaleByPowerOfTen(-2);
System.out.printf("\t\t%-16s %11.2f\n", category, dollars);
}
}
/**
* The entry point for the program - takes user input, builds an
* Advertisement, and displays its cost.
*/
public class EntryPoint
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome!");
int wordCount = readWordCount(scanner);
LinkPlan linkPlan = readLinkPlan(scanner);
Graphic graphic = readGraphic(scanner);
Advertisement advert = new Advertisement(wordCount, linkPlan, graphic);
advert.printAllCosts();
}
private static int readWordCount(Scanner scanner)
{
System.out.print("Enter the number of words in your ad: ");
// Could add validation code in here
return scanner.nextInt();
}
private static LinkPlan readLinkPlan(Scanner scanner)
{
System.out.print("Would you like to add a link (y = yes or n = no)? ");
char addLink = readSingleCharacter(scanner, "yn");
LinkPlan linkPlan;
if (addLink == 'n')
{
return LinkPlan.NONE;
}
System.out.print("Would you like to pay 3 months in advance " +
"(y = yes or n = no)? ");
char advancePay = readSingleCharacter(scanner, "yn");
return advancePay == 'y' ? LinkPlan.PREPAID : LinkPlan.POSTPAID;
}
private static Graphic readGraphic(Scanner scanner)
{
System.out.print("Would you like to add graphics/pictures? " +
"(s = small, m = medium, l = large or n = None)? ");
char graphic = readSingleCharacter(scanner, "smln");
switch (graphic)
{
case 's': return Graphic.SMALL;
case 'm': return Graphic.MEDIUM;
case 'l': return Graphic.LARGE;
case 'n': return Graphic.NONE;
default:
throw new IllegalStateException("Unexpected state; graphic=" +
graphic);
}
}
private static char readSingleCharacter(Scanner scanner,
String validOptions)
{
while(true)
{
String input = scanner.next();
if (input.length() != 1 || !validOptions.contains(input))
{
System.out.print("Invalid value. Please try again: ");
continue;
}
return input.charAt(0);
}
}
}
linkCost is not initialized when link == 'y' and advPay is not 'y' or 'n'.
In other words, you get this error when the compiler can find a path through your code where a local variable is not initialized before it is used.
this happens because the assignment occurs inside a conditional, if the condition is not met, the assignment never occurs
to avoid the error you have to assign a value (the most common would be 0) outside the conditional.
The analysis Eclipse performs to determine whether the variable is assigned on every code path isn't intelligent enough to realize that the tests on numberOfWords will never all fail. Typically, because it's not possible to statically evaluate every possible condition, a compiler/syntax checker won't attempt to evaluate any of them. If you replaced the last "else if" with an "else" it should work, as at least one assignment to textCost will occur regardless of the conditions being tested.
Eclipse is warning you because your initializations are happening inside conditionals. If none of the conditions are satisfied, textCost will be uninitialized.
if (numberOfWords <= 25)
{
//assign a value to textCost
}
else if (numberOfWords <= 35)
{
//assign a value to textCost
}
else if (numberOfWords > 35)
{
//assign a value to textCost
}
Eclipse probably isn't recognizing that (numberOfWords <= 35) and (numberOfWords > 35) cover all possibilities.
You could either initialize it to 0 on declaration, or include an additional else {} which sets it to zero.
Similar explanation for the other variable.
the error message tells you, that those variables aren't always initialized. This is, because your initialization happens only under certain conditions (they are located in if-statements). Hope this helps..
Even though you know one of the 3 branches of the comparison with numberOfWords will be visited, the compiler doesn't know that. It will wrongly assume that it is possible to enter the else clause and the textCost variable will remain unitiliazed.
Similarly with the switch (advPay).
Even though you know that one of the two will be visited, the compiler doesn't.
Suggestion:
Remove the else if (numberOfWords > 35) make it just an else.
As for the switch (advPay), add a default case. Inside you can put a throw new AssertionError();.
A good way to avoid such issues is to set the to be assigned variable as final and uninitialized before the checks. This will force you to set a value before you can use/read it.
final textCostTmp;
if (condition1) {
textCostTmp = ...;
} else if (condition2) {
textCostTmp = ...;
}
// if you use textCostTmp here the compiler will complain that it is uninitialized !
textCost = textCostTmp;
To solve this DO NOT initialize the variable as you may miss the missing else case.
The only proper solution is to add an else case to cover all possible cases !
I consider it bad practice to initialize non final variables except for some rare scenarios like a counter in a loop.
The proposed approach will force you to handle all possible cases, now and in the future (easier to maintain).
The compiler is a bit stupid at times (cannot figure that numberOfWords > 35 is the else)...but the compiler is your ally not your enemy...

Categories