d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Beginner Using Mod Operator( Java) > Topic
12Next
Add Reply New Topic New Poll
Member
Posts: 2,702
Joined: Jan 5 2008
Gold: 0.49
Oct 7 2013 07:13pm
i am new to programing and i am very confused how to do this

how would i go into getting this using the mod operator?


Company: The Ramjac Corporation

Shares Held: 100

Opening Price per Share: 37 5/8

Opening Portfolio Value: $3762.50

Change in Share Price: 7 2/8

Closing Price per Share: 44 7/8

Closing Portfolio Value: $4487.50

Change in Share Price: 4 4/8

Closing Price per Share: 49 3/8

Closing Portfolio Value: $4937.50
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 7 2013 08:03pm
im not sure what you're trying to do. but mod is (for the most part) the remainder. just write out your math expressions and use % instead of "mod". i'm not clear why you need it at all for that.

This post was edited by carteblanche on Oct 7 2013 08:04pm
Member
Posts: 2,702
Joined: Jan 5 2008
Gold: 0.49
Oct 7 2013 09:35pm
What would be the code for this though? Can u get me started
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 7 2013 09:46pm
i've seen a lot of intro level java assignments, but i've never seen one talk about stocks. Not to mention you're a little older than the typical intro-to-java student. what exactly are you trying to code? are you just asking the user for input then calculating a few things and spitting it back out? or are you really trying to grab legit stock info?
Member
Posts: 2,702
Joined: Jan 5 2008
Gold: 0.49
Oct 8 2013 06:45am
Input, then calculating . And I have to code some sort of way to make the change using the mod operator

this is the full assignment.

Programming Assignment #4

(Integer Arithmetic and Interactive Input)

Several years ago, the major American stock exchanges began reporting stock prices as decimal values (i.e., dollars and cents), as is common practice in all other institutions.

Previously, however, – in a tradition dating back to the Spanish conquistadores - prices had been calibrated to the nearest eighth (or sixteenth, thirty-second, etc.) of a dollar.

This assignment is to write a class to represent a stock portfolio using the traditional measure, and another class that tests it.


I. The StockPortfolio Class

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

Naturally, your class will have a constructor that initializes the instance variables to arguments passed when the object is created

Other methods of the class will include

1. “Get” methods for each of the instance variables
2. A method to modify the stock price. This method will have 2 int parameters:
• The change in the dollar portion of the price
• The change in the eighths portion of the price
3. A method to compute and return the portfolio value as a decimal

II. The Test Class (aka: “Client Code”)

Your test class will do the following:

1. Have the user enter the data (company name, number of shares, dollars, eighths)
2. Create a StockPortfolio object
3. Call the “get” methods to access the object data, and then print it
4. Get and print the portfolio value as a decimal


5. Have the user enter the change in the share price - in dollars and eighths - and print it
6. Get and print the updated share price
7. Get and print the updated portfolio value
8. Have the user enter another change in the share price - in dollars and eighths - and print it
9. Get and print the updated share price
10. Get and print the updated portfolio value

Here is some sample output:

Company: The Ramjac Corporation

Shares Held: 100

Opening Price per Share: 37 5/8

Opening Portfolio Value: $3762.50

Change in Share Price: 7 2/8

Closing Price per Share: 44 7/8

Closing Portfolio Value: $4487.50

Change in Share Price: 4 4/8

Closing Price per Share: 49 3/8

Closing Portfolio Value: $4937.50

Your output need not look exactly like this but must include all the information shown above, neatly presented and labeled, with “dollar and cents” figures preceded by a dollar sign ($)

III. Additional specifications:

1. You may assume that the price change is always positive (we should live so long!)
2. Although your program must work for any reasonable data, use the data shown above in the run you hand in.
3. Include a comment block above the definition of your StockPortfolio class containing your name and section, the required affirmation, and a brief description of the problem.
4. All “dollars and cents” figures must be displayed rounded to 2 decimal places

IV. Hint

See ChangeMaker.java and review the int division and mod operations


This post was edited by Yohancy on Oct 8 2013 06:51am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 8 2013 08:02am
IV. Hint

See ChangeMaker.java and review the int division and mod operations

I don't know how to turn 48 11/8 into 49 3/8 in the modifyStockPrice function (I'm Canadian so I've never used fractions in my life) but it probably has something to do with the hint and somethign you did in ChangeMaker

StockPortfolio.java
Code
public class StockPortfolio {

private String companyName;

private int shares;

private int dollar;

private int eighths;

public StockPortfolio( String companyName, int shares, int dollar, int eighths ) {

this.companyName = companyName;
this.shares = shares;
this.dollar = dollar;
this.eighths = eighths;
}

public String getCompanyName( ) {
return companyName;
}

public int getShares( ) {
return shares;
}

public int getDollar( ) {
return dollar;
}

public int getEighths( ) {
return eighths;
}

public void modifyStockPrice( int dollar, int eighths ) {
this.dollar += dollar;
this.eighths += eighths;
}

public double computePortfolioValue( ) {
return shares * ((double) dollar + ((double) eighths / 8));
}

}


Main.java
Code
import java.text.NumberFormat;
import java.util.Scanner;

public class Main {

public static void main( String[] args ) {

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance( );

Scanner scanner = new Scanner( System.in );
System.out.println( "Company name: " );
String name = scanner.nextLine( );
System.out.println( "Number of shares: " );
int shares = scanner.nextInt( );
System.out.println( "Dollars: " );
int dollar = scanner.nextInt( );
System.out.println( "Eighths: " );
int eighths = scanner.nextInt( );

/* 2. Create a StockPortfolio object */
StockPortfolio stockPortfolio = new StockPortfolio( name, shares, dollar,
eighths );

/* 3. Call the “get” methods to access the object data, and then print it */
System.out.println( "Company: " + stockPortfolio.getCompanyName( ) );
System.out.println( "Shares held: " + stockPortfolio.getShares( ) );
System.out.println( "Opening price per share: "
+ stockPortfolio.getDollar( ) + " " + stockPortfolio.getEighths( )
+ "/8" );

/* 4. Get and print the portfolio value as a decimal */

System.out.println( "Portfolio value: "
+ currencyFormat.format(
stockPortfolio.computePortfolioValue( ) ) );

for ( int i = 0; i < 2; i++ ) {
/*
* 5. Have the user enter the change in the share price - in dollars and
* eighths - and print it
*/
System.out.println( "Dollars: " );
dollar = scanner.nextInt( );
System.out.println( "Eighths: " );
eighths = scanner.nextInt( );
stockPortfolio.modifyStockPrice( dollar, eighths );
System.out.println( "Dollars: " + stockPortfolio.getDollar( ) );
System.out.println( "Eighths: " + stockPortfolio.getEighths( ) );

/* 6. Get and print the updated share price */
System.out.println( "Change in share price: " + dollar + " " + eighths
+ "/8" );

System.out.println( "Closing price per share: "
+ stockPortfolio.getDollar( ) + " " + stockPortfolio.getEighths( )
+ "/8" );

/* 7. Get and print the updated portfolio value */
System.out.println( "Closing portfolio value: "
+ currencyFormat.format( stockPortfolio.computePortfolioValue( ) ) );
}
}
}


This post was edited by labatymo on Oct 8 2013 08:03am
Member
Posts: 1,967
Joined: Jul 10 2007
Gold: 1,252.00
Oct 8 2013 09:07pm
I imagine ChangeMaker.java would explain everything you need to know. But anyways...

Using your example "turn 48 11/8 into 49 3/8"

For now disregard the 48 and focus solely on the fractional component. How many dollars is 11/8? If you evaluate the fraction using "human" division there are 11.375 dollars in 11/8. But your class does not maintain decimal components it maintains two integers to represent whole dollars and eighths of a dollar. If you simplify the fraction into a mixed number you have 1 3/8, which is comparable to the format that your class maintains share prices in. dollar = 1 and eighths = 3, but how did we arrive at this conclusion. First we evaluate how many whole dollars are in 11/8. In Java if you divide two integers any non integer component (ie decimals) are thrown away, so 11 ÷ 8 = 1. Now how do we know how many eighths of a dollar we have in 11/8 that aren't part of a whole dollar? Like carteblanche said in his first post, mod basically gives you the remainder. So 11%8 will give you a result of 3. So by evaluating 11 ÷ 8 we have determined we have 1 whole dollar in 11/8 and by evaluating 11/%8 we determined we have 3/8 leftover. When you consider the $48 we have been ignoring thus far, you need to add the whole dollar and set eights = 3, so dollar = 48 + 1 = 49 and eighths = 3.

Using the example from above:

1. Closing Price per Share: 44 7/8

2. Change in Share Price: 4 4/8

3. Closing Price per Share: 49 3/8

At step 1. stockPortfolio.dollar = 44 and stockPortfolio.eighths = 7

On step 2. stockPortfolio.modifyStockPrice(4, 4) is called.

On Step 3. the expected values are stockPortfolio.dollar = 49 and stockPortfolio.eighths = 3, but being that your modifyStockPrice method is defined as:

Code
public void modifyStockPrice( int dollar, int eighths ) {
this.dollar += dollar;
this.eighths += eighths;
}

[/CODE]

The actual result is stockPortfolio.dollar = 48 and stockPortfolio.eighths = 11. To get the expected result, you need to evaluate how many eighths there are and convert them to dollars and add to dollar and set the remainder to eighths.
Member
Posts: 2,702
Joined: Jan 5 2008
Gold: 0.49
Oct 10 2013 09:34am
This is what i am doing and my program will not run for some reason..

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Yohancy
*/
public class StockPortfolio {


int sharesheld;
int dollarportion ;
int eightsportion ;
int modchange;
int changeclosingeights;
int totalint;
int closingprice;
int changepriceDollar;



private String name;

public StockPortfolio(String thename, int thesharesheld, int thedollarportion , int theeightsportion)
{
sharesheld = thesharesheld;
dollarportion = thedollarportion;
eightsportion = theeightsportion;
name = thename;
}


public String getName()
{
return name ;
}
public int getsharesheld()
{
return sharesheld ;
}
public int getdollarportion()
{
return dollarportion ;
}

public int geteightsportion()
{
return eightsportion ;
}



public void modifyStockPrice( int dollar, int eights ) {

this.dollarportion = dollar;
this.eightsportion = eights;
}

public void computeClosing () { //calculate change in price



if (dollarportion + this.dollarportion >8 ){

changepriceDollar = (dollarportion + this.dollarportion);
changeclosingeights = (eightsportion + this.eightsportion );
modchange = ( changeclosingeights %8);
totalint = ( changeclosingeights / 8);
closingprice = ( sharesheld * ( changeclosingeights + totalint ));

System.out.println("\n Change in Share price" + dollarportion + this.eightsportion+ "/"
+ ("\nOpening Price per Share:"+ dollarportion
+ eightsportion + "/8")
+ "\n Closing Portfolio Value: " + closingprice)
;






}
else

changepriceDollar = (dollarportion + this.dollarportion);
changeclosingeights = (eightsportion + this.eightsportion );
closingprice = ( sharesheld * ( changeclosingeights + totalint ));
totalint = ( changeclosingeights / 8);

System.out.println("\n Change in Share price" + dollarportion + this.eightsportion+ "/"
+ ("\nOpening Price per Share:"+ dollarportion
+ eightsportion + "/8")
+ "\n Closing Portfolio Value: " + closingprice);












}






}



And this is the test class:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JOptionPane ;
/**
*
* @author Yohancy
*/
public class Client {

public static void main (String args[])
{

int sharesheld;
int dollarportion ;
int eightsportion ;
String name;
String input = null;






name = JOptionPane.showInputDialog
(null, "What is the name of the company?", "Name",
JOptionPane.QUESTION_MESSAGE) ;

eightsportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the name eights amount?", "Eights",
JOptionPane.QUESTION_MESSAGE) ;

dollarportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the name dollar ammount?", "Dollar",
JOptionPane.QUESTION_MESSAGE) ;

sharesheld = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the shares held ?", "Shares",
JOptionPane.QUESTION_MESSAGE) ;




StockPortfolio StockPortfolio = new StockPortfolio( name , sharesheld , dollarportion , eightsportion);

name = StockPortfolio.getName();
sharesheld = StockPortfolio.getsharesheld();
dollarportion = StockPortfolio.getdollarportion();
eightsportion = StockPortfolio.geteightsportion();

System.out.println("Company:" + StockPortfolio.getName()
+ "\nShares Held: " + StockPortfolio.getsharesheld()
+ ("\nOpening Price per Share:"+ StockPortfolio.getdollarportion()
+ StockPortfolio.geteightsportion() + "/8")
+ "\n Opening Portfolio Value:" + (sharesheld * (dollarportion + eightsportion)))
;


eightsportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the new eights amount?", "Eights",
JOptionPane.QUESTION_MESSAGE) ;

dollarportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the new name dollar ammount?", "Dollar",
JOptionPane.QUESTION_MESSAGE) ;

StockPortfolio.modifyStockPrice(dollarportion , eightsportion);

StockPortfolio.computeClosing ();

eightsportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the new eights amount?", "Eights",
JOptionPane.QUESTION_MESSAGE) ;

dollarportion = Integer.parseInt(input);
input = JOptionPane.showInputDialog
(null, "What is the new name dollar ammount?", "Dollar",
JOptionPane.QUESTION_MESSAGE) ;

StockPortfolio.modifyStockPrice(dollarportion , eightsportion);

StockPortfolio.computeClosing ();


















}


}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 10 2013 09:43am
what does "will not run" mean? compile error? runtime error?

idk if you can use the JOptionPane without a frame. never tried. typically if you wanna use GUI, you should use a GUI

This post was edited by carteblanche on Oct 10 2013 09:46am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 10 2013 09:52am
Code
eightsportion = Integer.parseInt( input );
input = JOptionPane.showInputDialog( null,
"What is the name eights amount?", "Eights",
JOptionPane.QUESTION_MESSAGE );


is where your first problem is

Code
eightsportion = Integer.parseInt( input );


should come after

Code
input = JOptionPane.showInputDialog( null,
"What is the name eights amount?", "Eights",
JOptionPane.QUESTION_MESSAGE );



You also need to fix the same problem every time you pass JOptionPane.showInputDialog() to input
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll