Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java without using any loop or if else or while just if 1. Create a class called

ID: 3754091 • Letter: J

Question

java without using any loop or if else or while
just if

1. Create a class called Stock that can be used to handle stock marker operations. Your class should contain the following: A string data field named symbol for the stock's symbol . A string data field named name for the stock's name .A double data field name previousClosingPrice that stores the stock price for the previous day .A double data field named currentPrice hat stores the stock price for the current time . A constructor that creates a stock with specified symbol and name . The set method for all data fields .The get method for all data fields . A method named changePercent that returns the percentage changed from to surrentPrics Write a test program that creates two Stock class objects with the following information: Stock symbol ABC, the name NameInc, and the previous closing price of 60. Set a new current price to 65 and display the price-change percentage. . Stock symbol BCC, the name News Inc., and the previous closing price of 100. Set a new current price to 110 and display the price-change percentage. Sample Output: Stock Information: Symbol: ABC Name: Name Inc. Previous Closing Price: 60.00 Current Price: 65.00 Price-changed Percentage: 8.33 % Symbol: BCC Name: News Inc. Previous Closing Price: 100.00 Current Price: 110.00

Explanation / Answer

class Stock

{

private String symbol; // initializing String type data to store Symbol

private String name; // initializing String type data to store name

private double previousClosingPrice; // initializing double type data to store previous closing Price

private double currentPrice; // intializing double type data to store current price

  

// Constructor takes the same name as of the class hence below is the definition of the constructor Stock

// this keyword is used when we are referring to the current object whose constructor is called.

Stock(String symbol, String name){  

this.symbol = symbol; // Setting up the symbol

this.name=name; // Setting up the name

}  

///-------------------- Method to set the previous closing Price-----------

public void setpreviousClosingPrice ( double x ) {

this.previousClosingPrice = x; // Setting up the price

}

// ----------------- Method to set the current price--------------------

public void setcurrentPrice(double y)

{

this.currentPrice = y; // Setting up the price

}

//-------------- Method to get the Symbol------------------

public String getSymbol()

{

return symbol;

}

//-------------- Method to get the name------------------

public String getName()

{

return name;

}

//-------------- Method to get the previous Closing Price------------------

public double getpreviousClosingPrice()

{

return previousClosingPrice;

}

//-------------- Method to get the Current Price------------------

public double getcurrentPrice()

{

return currentPrice;

}

//-------------- Method to get the changed percentage------------------

public double changePercent()

{

double gap = currentPrice - previousClosingPrice;

double change = (gap/previousClosingPrice)*100.00;

return Math.round(change * 100.0) / 100.0; ///// Rounding off the double to two digits using Math.round function and returning the result

}

//// Main Function - This is the function which runs first during the execution

public static void main (String[] args) throws java.lang.Exception  

{

// Creating an object s1 of the class Stock and initializing values using the constructor

// Constructor gets called whenever we create a new object

Stock s1= new Stock("ABC","Name Inc.");   

// Printing Symbol using getSymbol Function called through s1

System.out.println("Symbol: " + s1.getSymbol());

// printing Name using getName function called through s1

System.out.println("Name: " + s1.getName());

// Setting up the previous closing price using s1 and printing it

s1.setpreviousClosingPrice(60.00);

System.out.println("Previous Closing Price: " + s1.getpreviousClosingPrice());

// Setting up the Current price using s1 and printing it

s1.setcurrentPrice(65.00); // calling set function

System.out.println("Current Price: " + s1.getcurrentPrice() );

// calling the change Percent function

System.out.println("Price-Changed Percentage: " + s1.changePercent() + "% ");

//// ----------------- Creating one more object s2 of class Stock and again performing the same functions

Stock s2= new Stock("BCC","News Inc.");

System.out.println("Symbol: " + s2.getSymbol());

System.out.println("Name: " + s2.getName());

s2.setpreviousClosingPrice(100.00);

System.out.println("Previous Closing Price: " + s2.getpreviousClosingPrice());

s2.setcurrentPrice(110.00);

System.out.println("Current Price: " + s2.getcurrentPrice() );

System.out.println("Price-Changed Percentage: " + s2.changePercent()+"%");

}

}