1 / 6100%
CSE 110 - Lab 8
This lab deals with the basic concepts of object oriented programming including classes, object creation,
get and set methods and function calling through an object.
Use the following Coding Guidelines:
When declaring a variable, you usually want to initialize it.
Use white space to make your program more readable.
Use comments after the ending brace of classes, methods, and blocks to identify to which block it
belongs.
Assignments Documentation:
At the beginning of each programming assignment you must have a comment block with the
following information:
/*---------------------------------------------------------------------------
// AUTHOR: (Put your name here)
// FILENAME: Lab8.java
// SPECIFICATION: This program is for practicing the object oriented programming.
// LAB LETTER: (Put your Lab Letter here)
//-------------------------------------------------------------------------*/
Getting Started
Create a class called Lab8. Use the same setup for setting up your class and main method as you did
for the previous assignments. Be sure to name your file Lab8.java.
Hints
Please replace //--> with the correct program to finish the task according to the corresponding comment.
Steps to be followed:
a. If you are using Eclipse:-
Create a new project in Eclipse called Lab8. Add two classes to this project namely Lab8 and
FinancePortfolio. The class Lab8 must contain the main block. Do not add the main block in the
FinancePortfolio class.
b. If you are using Textpad:-
Create two .java files namely Lab8 and FinancePortfolio. The file Lab8 must contain the main
block. Do not add the main block in the FinancePortfolio class.
FinancePortfolio Class:
public class FinancePortfolio {
private double totalFunds,stocks,mutualFund,Bonds;// define the double
variables for totalFunds, stocks,mutualFund,Bonds
private String name;// define a String variable name.
private int age;// define an int variable age
public FinancePortfolio()
{
// default constructor. Initialize all the double and integer values to 0 and
String value to null.
}
public int createPortfolio(String n, int b, double funds,double stc,double
mfund,double bn)
{
// create a method which first calls the check method to see if it matches the
criteria (age>=18 and investment amount>=$25000)
//If check returns a "true", assign the values n,b and funds to name,age and
totalFunds respectively. Return a random variable between 1 to 100 which is the user
id.
//Next check if stc+mfund+bn=100 i.e. the percentage of portfolio division
=100.
//If yes, then call the functions getStocks(), getMutualFunds(),
getBonds and assign them to variables stocks,mutualFund and Bonds respectively.Print
a statement that says"Diversification of funds successful"
//else print "Please check the Diversification of percentage.
Diversification unsuccessful."
//if check returns a false the print "The customer is not eligible to apply"
}
private boolean check(int a, double sum)
{
// check if value to invest is above $25,000 and investor age is above 18
}
private double getStocks(double total,double e)
{
//get the value of the stocks in $
}
private double getMutualFunds(double total,double e)
{
// get the value of the Mutual Funds in $
}
private double getBonds(double total,double e)
{
// get the value of the Bonds in $
}
public void display()
{
System.out.println("User Details: \n");
// Print user details.
}
}
Lab8.java Class:
import java.util.Scanner;
public class Lab8 {
public static void main(String[] args) {
double totalFunds,stocks,mutualFund,Bonds; // define variables
String name;//define these variable
int age;//define these variables
int choice;
//create an object fp of class FinancePortfolio
//-> create a scanner object in.
do{
String linebreak; // define a String variable linebreak
System.out.println("Please choose one of the following:\n1. Create a
Portfolio \n2. Display the created portfolio \n3. Quit");
choice= in.nextInt();
linebreak=in.nextLine();
switch(choice){
case 1:
System.out.println("Input the Name of the Customer");
name=in.nextLine();
System.out.println("Input the Age of the Customer");
age=in.nextInt();
linebreak=in.nextLine();
System.out.println("Input the Total Fund to be Invested");
totalFunds=in.nextDouble();
linebreak=in.nextLine();
System.out.println("Input the Percentage of Fund to be Invested
in Stocks");
stocks=in.nextDouble();
linebreak=in.nextLine();
System.out.println("Input the Percentage of Fund to be Invested
in Mutual Funds");
mutualFund=in.nextDouble();
linebreak=in.nextLine();
System.out.println("Input the Percentage of Fund to be Invested
in Bonds");
Bonds=in.nextDouble();
linebreak=in.nextLine();
//--> Call the function createPortfolio of the class
FinancePortfolio and pass the above variables as parameters.
//--> Create a variable int j and assign the output of the above
function to it.
//--> print the statement "The Id of the customer is :" +j
break;
case 2:
//->call the display function
}
}
while(choice!=3);
in.close();
}
}
Output Runs:-
1) Default Constructor:-
2) Age Below 18:-
3) Funds Below $25000:-
4) Fund Diversification Unsuccessful as value of division doesnt add up to 100:-
5) Portfolio Created and Displayed Successfully:-
Powered by TCPDF (www.tcpdf.org)
Students also viewed