Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Java Data Structrure, STACK public class Auto { private Engine engine; private String model; private String color; private int speed; public Auto(Engine engine, String model,

Java Data Structrure, "STACK"

public class Auto { private Engine engine; private String model; private String color; private int speed;

public Auto(Engine engine, String model, String color, int speed) { this.engine = engine; this.model = model; this.color = color; this.speed = speed; }

public void speedUp(int speedIncrease){ this.speed = this.speed + speedIncrease; this.engine.speedUp(speedIncrease); } public void slowDown(int speedDecrease){ this.speed = this.speed - speedDecrease; this.engine.slowDown(speedDecrease); } public void printInfo(){ System.out.println("======================"); System.out.println("Model: " + this.model); this.engine.printInfo(); System.out.println("Color: " + this.color); System.out.println("Speed: " + this.speed); System.out.println("======================"); } }

public class Engine { private String model; private double volume; private int maxrpm; private int rpm;

public Engine(String model, double volume, int maxrpm, int rpm) { this.model = model; this.volume = volume; this.maxrpm = maxrpm; this.rpm = rpm; }

public void printInfo(){ System.out.println("Engine model: " + model); System.out.println("Engine volume: " + volume); System.out.println("Engine maxrpm: " + maxrpm); System.out.println("Engine current rpm: " + rpm); } public void speedUp(int speed){ int increaseRpm = speed * 10; this.rpm = this.rpm + increaseRpm; } public void slowDown(int speed){ int decreaseRpm = speed * 10; this.rpm = this.rpm - decreaseRpm; } }

public class TestProgram {

public static void main(String[] args) { // Auto[] autos = new Auto[5]; Auto a1 = new Auto(null,"W", "red",110); Auto a2 = new Auto(null, "B", "red",120); Auto a3 = new Auto(null,"M", "red",130); Auto a4 = new Auto(null,"F", "yellow",140); LinkedList myAutos= new LinkedList(); myAutos.insert(a1, myAutos.zeroth()); myAutos.insert(a2, myAutos.zeroth()); myAutos.insert(a3, myAutos.zeroth()); myAutos.insert(a4, myAutos.zeroth()); System.out.println(" First View: "+myAutos ); LinkedList.printList(myAutos); // LinkedListIterator itr= myAutos.findLast(a4); }

}

Add to the ADT for both implementations that we have defined(Array and Linked List) the following methods: ShowElements: shows all the elements in the stack ShowInverse: show the elements in inverse order. New constructor which specifies size of stack (for the array implementation) as parameter. Clone: replicate a stack in another stack. Swap: exchange the two topmost items on the stack. Test all these in a testing class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions