hey I am having some difficulties with a project relating stack. i have a stack
ID: 3604945 • Letter: H
Question
hey I am having some difficulties with a project relating stack. i have a stack class name MyStack with all the push, pop and top method which throws their respective overflow and underflow exception.
i also have a sales class which has a string and double instance variables. a constructor to initialize them as well as two getter methods contaning no parameters which returs the aforementioned string and double variable.
I need to now make buyer class which needs:
a string variable and MyStack member variable to store sales objects, constructor to initialize the stack with 2 sales max/buyer, setter and getter to set and return their names, a method with sales parameter to include to buyer stack, another method XthSales that takes an integer x as a parameter ans returns xth sales in a user's stack
how do i go about doing this?
Explanation / Answer
class buyer {
String str;
MyStack stack;
buyer () {
stack = new MyStack();
//Create two Sales object and add to MyStack
//I dont know what is there is sales class
sales s = new sales("Paper",10.7);
sales s2 = new sales("Oil",100.7);
stack.push(s);
stack.push(s2);
}
String get_name(){
return name;
}
String set_name(String s){
name = s;
}
public sales XthSales(int p){
int i = 0;
while (!stack.isEmpty()){
i = i + 1;
sales s = stack.pop();
if(i==p)
return s
}
return null;
}
}
You can go about this way, Note: Syntax might differ because I dont have the actual code. The whole idea is to go about this way ,Create stack, add sales. manipuates sales object etc
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.