Question

In: Computer Science

A system of caves is a group of connected underground tunnels. Design an ADT for a...

A system of caves is a group of connected underground tunnels. Design an ADT for a cave and a cave system. An archaeologist should be able to add a newly discovered cave to a cave system. He/she can enter this system through only one cave and exit the system only through a different cave. Duplicate caves – based on GPS coordinates – are not permitted. Archaeologists should also be able to list the caves in a given cave system.

File Submission

• Cave.java

Cave object containing Cave Name and Cave GPS coordinates (Latitude and Longitude) and any other appropriate attributes if needed

• CaveInterface.java

Interface containing method definition to add and remove caves

• CaveStack.java

Stack implementation of CaveInterface.java

• CaveStackDemo.java

Demonstrate adding, removing and listing caves. Prompt the user for various cave names and GPS coordinates to show your implementation.

Solutions

Expert Solution

Ans: Since It is asked develop the system in the form of a stack hence removeStack() works as that of a stack but it is also over-ridden and can also be used to remove a particular cave from in between.

Please read comments in the code for better understanding.

import java.util.*;

// --------x Cave.java x---------
class Coordinates{
    double latitude;
    double longitude;
}

class Cave {
    String caveName;
    Coordinates coord;
    
    public Cave(){
        caveName = "";
        coord = new Coordinates();
    }
}

// --------x CaveInterface.java x---------
interface CaveInterface{
    void addCaves(Cave c);
    void removeCave();
    void removeCave(Cave c);
}

// --------x CaveStack.java x---------
class CaveStack implements CaveInterface {
    Stack<Cave> caveSys;
    HashSet<String> caveSet;
    
    public CaveStack(){
        caveSys = new Stack<Cave>();
        caveSet = new HashSet();
    }
    
    // Add unique Caves
    public void addCaves(Cave c){
        // Storing coordinates as string
        String coordinates = c.coord.latitude + " "+c.coord.longitude;

        if(caveSet.contains(coordinates) == false){
            caveSys.push(c);
            caveSet.add(coordinates);
        }
    }
    
    // Remove the top cave from Cave stack
    public void removeCave(){
        
        if(caveSys.empty() == false){
            Coordinates cd = caveSys.peek().coord;
            // Get coordinate string
            String coordinates = cd.latitude + " "+cd.longitude;
            caveSys.pop();
            caveSet.remove(cd);
        }
    }
    
    // Remove a given Cave from the Cave System
    public void removeCave(Cave c){
        int location = caveSys.search(c);
        
        if(caveSys.empty() == false && location != -1){
            Cave temp;
            Stack<Cave> tempStack = new Stack();
            
            // Reach the cave
            while(location > 1){
                temp = caveSys.peek();
                caveSys.pop();
                tempStack.push(temp);
                location--;
            }
            
            // Remove the cave
            String coordinates = c.coord.latitude + " "+c.coord.longitude;
            caveSys.pop();
            caveSet.remove(coordinates);
            
            // Restore the rest of the cave
            while(tempStack.empty() == false){
                temp = tempStack.peek();
                tempStack.pop();
                caveSys.push(temp);
            }
        }
    }
    
    // Lists all the caves present in the given Cave System
    public void listCaves(){
        
        caveSys.forEach(cave -> {
            System.out.println("Name: "+ cave.caveName);
            System.out.println("Coordinates are :-");
            System.out.print("Latitude: "+cave.coord.latitude+" ");
            System.out.println("Longitude: "+cave.coord.longitude);
        });
    }
}

// --------x CaveStackDemo.java x---------
class CaveStackDemo{
    
    // Driver Method
    public static void main(String args[]){
        // First Input
        Scanner sc = new Scanner(System.in); 
        
        Cave c1 = new Cave();
        Cave c2 = new Cave();
        // Reads string before the space
        System.out.print("Enter cave name: ");
        c1.caveName = sc.next();
        System.out.print("Enter Latitude: ");
        c1.coord.latitude = sc.nextDouble();
        System.out.print("Enter Longitude: ");
        c1.coord.longitude = sc.nextDouble();
        
        System.out.print("Enter cave name: ");
        c2.caveName = sc.next();
        System.out.print("Enter Latitude: ");
        c2.coord.latitude = sc.nextDouble();
        System.out.print("Enter Longitude: ");
        c2.coord.longitude = sc.nextDouble();
        
        // Create new cave system
        CaveStack cs = new CaveStack();
        cs.addCaves(c1);
        cs.addCaves(c2);
        
        cs.listCaves();
        
        cs.removeCave();
        cs.listCaves();
    }
}

Related Solutions

A system of caves is a group of connected underground tunnels. Design an ADT for a...
A system of caves is a group of connected underground tunnels. Design an ADT for a cave and a cave system. An archaeologist should be able to add a newly discovered cave to a cave system. He/she can enter this system through only one cave and exit the system only through a different cave. Duplicate caves – based on GPS coordinates – are not permitted. Archaeologists should also be able to list the caves in a given cave system. (JAVA)
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
Computer Science Design and implement an ADT that represents a triangle. The data for the ADT...
Computer Science Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle's three angles. The data should be in the private section of the class that implements the ADT. Include at least two initialization operations; one that provides default values for the ADT's data, and another that sets this data to client-supplied values. These operations are the class's constructors. The ADT also...
In the design of a city water supply system, three reservoirs are connected by three pipes...
In the design of a city water supply system, three reservoirs are connected by three pipes which meet at the junction. Reservoir A needs to supply 0.0938 m3/s to lower reservoirs B (Elevation 150 m) and C (Elevation 140 m). Pipe 1 is 900 m long and 300 mm in diameter, pipe 2 is a 200 mm diameter pipe 387 m long, and pipe 3 is 200 mm in diameter and 700 m long. The roughness coefficient for all pipes...
A system for the storage of thermal energy is to be designed using an underground tank...
A system for the storage of thermal energy is to be designed using an underground tank of water. The tank is buried at a depth of 3 m and is a cube of 1 m on each side. The water in the tank is heated by circulating it through a solar energy collection system. A given heat input to the water may be assumed due to the solar energy flux. Characterize the design problem in terms of the fixed quantities...
A 45 MPH design speed roadway with a 2000 ADT has a 6’ shoulder with a...
A 45 MPH design speed roadway with a 2000 ADT has a 6’ shoulder with a 12:1 slope. The shoulder immediately drains into a roadside ditch with 4:1 sideslopes, a bottom width of 4’. The bottom of the ditch is 1.75 feet deeper than the edge of the shoulder. The face of a rock cut is located 7’ from the center line of the ditch, away from the roadway. Is the rock cut located within the clear zone?  
A 56.0-Ω resistor is connected in parallel with a 121.0-Ω resistor. This parallel group is connected...
A 56.0-Ω resistor is connected in parallel with a 121.0-Ω resistor. This parallel group is connected in series with a 22.0-Ω resistor. The total combination is connected across a 15.0-V battery. (a) Find the current in the 121.0-Ω resistor. A (b) Find the power dissipated in the 121.0-Ω resistor. W
Studies indicate that there are 16% maintenance-related shutdowns of the underground train system in a particular...
Studies indicate that there are 16% maintenance-related shutdowns of the underground train system in a particular country. If 100 underground trains are randomly chosen,                        (a) Find the probability that more than fifteen shutdowns will occur. (b) Find the probability that exactly twelve shutdowns will occur.   
Match the following strengths and weakness with (a) between-group design or (b) within-group design. ____ More...
Match the following strengths and weakness with (a) between-group design or (b) within-group design. ____ More susceptible to individual characteristics that might confound an association (e.g., age, gender, genetic susceptibility). ____ More susceptible to confounding from time-related factors (e.g., learning effects, external factors). ____ Outcome of interest is compared before and after the intervention in a single cohort.
Describe the differences between a nonequivalent comparison group design and a one-group pretest-posttest design. For each,...
Describe the differences between a nonequivalent comparison group design and a one-group pretest-posttest design. For each, give an example of a situation in which one might have to use that design rather than a strong experimental design. What is the critical aspect of your example that makes it impossible to use a strong experimental design?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT