Documente Academic
Documente Profesional
Documente Cultură
Answer all questions. programming? (2 marks) Procedural approach breaks program into smaller chunks and execute them in a sequential form. OOP is based on objects interacting with other objects through a message to get the work done. It has several key features such as encapsulation, inheritance and polymorphism.
2. What is a default constructor?
(1 mark) A default constructor is provided by the compiler if the user has not defined any constructor. It will set all the data fields to their zero values. It is usually without any parameters. (2 marks) Methods have the same name but the number of parameters are different.
4. Suppose x, y, and z are int variables and w and t are double variables. What is
(5 marks)
x=17; y = 15; x = x + y / 4; x = 17 + 15/4; x = 17 + 3; x = 20 (1) z = x % 3 + 4; z = 20 % 3 + 4; z = 2 + 4; z = 6 (1) w = 17 / 3 + 6.5; w = 17/3 + 6.5; w = 5 + 6.5 w = 11.5 (1) t = x / 4.0 + 15 % 4 - 3.5; t = 20/4.0 + 15%4 3.5 t = 5.00 + 3 3.5 t = 8.00 3.5 t = 4.5 (2)
5. Write the definition of a class that has the following properties: a. The name of the class is Stock. b. The class Stock has four instance variables: name of type String,
(20 marks)
c. The class Stock has the following methods: displayStock to display the values of the four instance variables. setName method to set the name setPreviousPrice method to set the previous price of a stock setClosingPrice method to set the closing price of a stock setNumberOfShares method to set the number of shares owned by the stock
getName value returning method to return the name getPreviousPrice value-returning method to return the previous price of the stock getClosingPrice - value-returning method to return the closing price of the stock getNumberOfShares value-returning method to return the number of shares owned by the stock percentGain value-returning method to return the change in the stock value from the previous closing price and todays closing price as a percentage. shareValues value-returning method to calculate and return the total values of the shares owned default constructor the default value of name is the empty string ; the default values of previousPrice, closingPrice and numberOfShares are 0. public class Stock { private String name; private double previousPrice; private double closingPrice; private int numberOfShares; public Stock() { name = ""; previousPrice = 0.0; closingPrice = 0.0; numberOfShares = 0; } public void displayStock() { System.out.println("Name: " + name); System.out.println("Previous Price: " + previousPrice); System.out.println("Closing Price: " + closingPrice); System.out.println("Number of Shares: " + numberOfShares); } public void setName(String nm) { name = nm; } public void setPreviousPrice(double pp) { previousPrice = pp; } public void setClosingPrice(double cp) { closingPrice = cp; }
public void setNumberOfShares(int ns) { numberOfShares = ns; } public String getName() { return name; } public double getPreviousPrice() { return previousPrice; } public double getClosingPrice() { return closingPrice; } public int getNumberOfShares() { return numberOfShares; } public double percentGain() { double pg; pg = ((closingPrice - previousPrice)/previousPrice) * 100; return pg; } public double shareValues() { double sv; sv = numberOfShares*closingPrice; return sv; } }