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
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()+"%");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.