0

I have an assignment due for a programming class that requires me to put all my variables as int's but needs me to put one of the values as a fraction but the result gives me 0 for the fraction and I dont know how to make it work without changing the value to a double which i cant do. How do i turn an int into a fraction? `

private String companyname;
private int stockamount;
private int currentprice;
private int eightsprice;



public StockPortfolio (String compName, int sAmount, int nPrice, int ePrice) 
{ 
  companyname = compName;
  stockamount = sAmount;
  currentprice = nPrice;
  eightsprice = ePrice;       
  this.newcprice = newcprice;
  this.new8price = new8price;
}

public String getName()
{
   return companyname;
}

   public int getStock()
{
   return stockamount;
}    

   public int getPricenorm()
{
   return currentprice;
}

   public int getPrice8th()
{
   return eightsprice;
 } 

   public int getNewPrice()
{
   return newcprice;
}

   public int getNew8Price()
{
   return new8price;
}

   private int newcprice;
   private int new8price;   

public void changePrice(int newcprice, int new8price)
{   
    currentprice = currentprice + newcprice;
    eightsprice = eightsprice + new8price;
}


public int getValue()

{
   return (getStock()*(getPricenorm()+getPrice8th())); 
}

    }

` This is the test class which i still havent finished because i just want to solve the fraction part out first.

public class StockPortfolioTest {

public static void main(String[] args) {
StockPortfolio portfolio = new StockPortfolio("The Ramjac Corporation", 100, 37, 5/8);
System.out.println("Company: " + portfolio.getName());
System.out.println("Shares Held: " + portfolio.getStock());
System.out.println("Opening price per sale: " + portfolio.getPricenorm() + " " + portfolio.getPrice8th());
portfolio.changePrice(7, 2);
System.out.println("Change in Share Price: " );
System.out.println("Closing price per share: " + portfolio.getPricenorm()+ " " + portfolio.getPrice8th());
System.out.println("Closing Portfolio Value: ");
}}

And these are my results for what i get so far

"Company: The Ramjac Corporation

Shares Held: 100

Opening price per sale: 37 0

Change in Share Price:

Closing price per share: 44 2

Closing Portfolio Value: "

Mind, i'm just starting programming and dont know much.

Also, another quick question, how do i reduce the amount of characters in a number so i could make the result come out as dollars and cents and not come out as $100.5 or 100.50452 for example but make it come out with only $100.50.

  • In Java, the result of 5/8 is an integer, because the two operands are both integers. You can get a fractional result by dividing real numbers, or by casting an integer to a real number. For example: `5.0/8` will be a fraction, and `((double) 5)/8` will be a fraction. – Andy Thomas Oct 22 '15 at 00:43
  • I tried that in the test class but it doesnt work, it wont even let me run it. – Rafael Leal-Mccormack Oct 22 '15 at 00:48
  • You've declared the last argument of StockPortfolio to be an int. If you want it to represent a fraction, you'll need to change the type to `float` or `double`. You should be getting an error message that describes the problem. – Andy Thomas Oct 22 '15 at 00:50
  • (Instance variables of the class will include 1. The name of the company 2. The number of shares held 3. The dollar portion of the share price 4. The eighths portion of the share price  To receive credit for this assignment, all instance variables except for the company name must be type int) Thats what the assignment says, thats why im having trouble understanding why they want a fraction – Rafael Leal-Mccormack Oct 22 '15 at 00:55
  • From the description, it sounds as if you're supposed to communicate the price as two values: the number of whole dollars, and the number of eighths of dollars in the remaining fraction. Both of those values are whole numbers (i.e., nonnegative integers). That said, if you feel the assignment needs clarification, the best person to ask is the instructor. – Andy Thomas Oct 22 '15 at 01:00
  • Thank you, I'll make sure to ask her. By any chance could you also tell me if theres a simple way to limit the amount of characters that come after a decimal? – Rafael Leal-Mccormack Oct 22 '15 at 01:10
  • `String.format("%.2f", value) ;` – Andy Thomas Oct 22 '15 at 01:27

0 Answers0