0

So I'm missing something here. I have a method that is supposed to compute and return the value of a stock portfolio. but it uses a mixed number which has a dollars portion and an eighths portion

public class StockPortfolio
{
  //Instance Vars 
  private String company;
  private int numOfShares;
  private int dollarSharePrice;
  private int eighthsSharePrice;

  public int portfolioValue()
  {
   int portVal = (dollarSharePrice * numOfShares) 
                 + (eighthsSharePrice * numOfShares) / 8;

   System.out.printf("Opening portfolio value: $%.2f" , portVal);

   return portVal;
  }

For the test class the values are 100 for numOfShares, 37 for dollarSharePrice and 5 for eighthsSharePrice.

The expected output should look like this:

Opening portfolio value: $3762.50

my real trouble is just get the .50 to print. I've tried using % in different ways but to no avail. any help pointing me in the right direction is appreciated. I am at my wits end here.

PeterLion
  • 5
  • 4
  • 1
    But you declare `portVal` as `int`... – Nir Alfasi Oct 16 '13 at 04:29
  • what should it be declared as? @alfasin – PeterLion Oct 16 '13 at 04:31
  • Fix the computation so that `portVal` is 376250. I'll bet you can figure out how to get the right output after that. Just make sure you do the division last. When you're working with integers, `(a * b) / c` is not the same as `a * (b / c)`. – ajb Oct 16 '13 at 04:33
  • @PeterLion - Have a look at my answer. – Rahul Oct 16 '13 at 04:49
  • @ajb I got `portVal` to get 376250. but I am getting a `IllegalFormatConversionException` – PeterLion Oct 16 '13 at 05:17
  • @PeterLion Are you using %f (or %.2f or something like that) in the format string? After you get 376250, you will need to pass two integers to `System.out.printf`, and your format string will have two %d specifiers in it. (P.S. I know there are other ways to do this such as R.J's answer, but I think it's instructive to see how to do this without any floating-point computation at all. Floating-point computations usually involve rounding, and for non-scientific applications you often want exact answers.) – ajb Oct 16 '13 at 14:46

2 Answers2

0
float portVal = (float)9/4;
System.out.printf("Opening portfolio value: $%.2f" , portVal);

OUTPUT:

Opening portfolio value: $2.25
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • The topic says "the instance variables can only be ints"; does that apply to local variables in methods too? – ajb Oct 16 '13 at 04:34
  • @ajb if you deal with ints only you can't expect to print a fraction unless you do all the calculation inline (inside `printf`) and cast to float... – Nir Alfasi Oct 16 '13 at 04:37
  • from instructions given it says all instance variables must be ints. so I'm assuming local variables can be of different type. – PeterLion Oct 16 '13 at 04:37
  • @alfasin Yes, you can get it to display the right answer without using any `float`s. I'm just not clear on whether this is an assignment that doesn't allow floats as local variables. – ajb Oct 16 '13 at 04:38
  • @ajb using `double` ? :) – Nir Alfasi Oct 16 '13 at 04:39
  • @alfasin See my comment up above. – ajb Oct 16 '13 at 04:39
  • @ajb even though it's correct - I can't see how it can help him in printing 0.5 or any other fraction without using a data type that support fractions (of course you can start calculating reminders and stuff - but this is way out of proportion to the talk IMHO). – Nir Alfasi Oct 16 '13 at 04:42
  • @ajb I'm going to assume for now everything has to be an int. including locals. So how would I go about this? and thanks in advance. – PeterLion Oct 16 '13 at 04:44
  • @PeterLion See my comment up above. You will call `printf` with two integers. – ajb Oct 16 '13 at 04:46
0

Don't use the portVal to print. Print it directly by doing something like this

System.out.printf("Opening portfolio value: $%.2f", (float)((dollarSharePrice * numOfShares) + (eighthsSharePrice * numOfShares) / 8));
Rahul
  • 44,383
  • 11
  • 84
  • 103