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.