Question: Convert a Loop into a Recursive Method *please comment** Find an example from a previous assignment where you used a loop to solve a problem.

Convert a Loop into a Recursive Method

*please comment**

Find an example from a previous assignment where you used a loop to solve a problem. After the loop, add a call to a recursive method that will accomplish the same function. Keep the original loop also.

The program should show the same results with the loop as with the recursive method.

Test it to make sure it works the same as with the loop.

CourseDirectory.java

package sandbox.course;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

public class CourseDirectory {

/**

* @param args the command line arguments

*/

static List courses = new ArrayList();

public static void main(String[] args) {

CourseDirectory myDirectory = new CourseDirectory();

myDirectory.createFIUCourses();

myDirectory.displayMenu();

}

public void createFIUCourses()

{

//Create a loop that will ask user if he/she wants to create another course - do while

//Inside loop, ask for all the information for a course

//Create another loop inside the outer loop (nested loop) for prerequisites (while)

//After the nested loop, create a Course object

//Add Course object to arrayList of courses

Scanner sc = new Scanner(System.in);

do

{

System.out.print("Enter FIU Course Name: ");

String FIUCourseName = sc.next();

System.out.print("Enter FLA Course Name: ");

String FLACourseName = sc.next();

System.out.print("Enter year level: ");

int yearLevel = sc.nextInt();

System.out.print("Enter number of credits: ");

int numCredits = sc.nextInt();

ArrayList preReqs = new ArrayList();

while(true)

{

System.out.print("Enter a pre-requisite for the course (Enter [Q/q] to stop entering prereqs): ");

String preReq = sc.next();

if(preReq.equalsIgnoreCase("q")) break;

preReqs.add(preReq);

}

Course course = new Course(FIUCourseName, FLACourseName, yearLevel, numCredits, preReqs);

courses.add(course);

System.out.print("Press [Q/q] to stop entering courses or any other key to continue entering: ");

String userInput = sc.next();

if(userInput.equalsIgnoreCase("q")) break;

}while(true);

}

public void displayMenu()

{

//Create a loop that displays the menu of options:

//1. Sort courses by FIU course name

//2. Sort courses by FLA course name

//3. Sort courses by year level

//4. Exit

//Keep looping until the user enters option 4

//For each of the options 1- 3, call a specific method

Scanner sc = new Scanner(System.in);

while(true)

{

System.out.println("1. Sort courses by FIU course name");

System.out.println("2. Sort courses by FLA course name");

System.out.println("3. Sort courses by year level");

System.out.println("4. Exit");

int userInput = sc.nextInt();

ComparatorByFLACourse comparatorByFLACourse = new ComparatorByFLACourse();

ComparatorByYearLevel comparatorByYearLevel = new ComparatorByYearLevel();

switch(userInput)

{

case 1: Collections.sort(courses); System.out.println(courses); break;

case 2: Collections.sort(courses, comparatorByFLACourse); System.out.println(courses); break;

case 3: Collections.sort(courses, comparatorByYearLevel); System.out.println(courses); break;

case 4: return;

}

}

}

}

ComparatorByFLACourse.java

package sandbox.course;

import java.util.Comparator;

class ComparatorByFLACourse implements Comparator

{

@Override

public int compare(Course c1, Course c2)

{

return c1.getFLACourseName().compareTo(c2.getFLACourseName());

}

}

ComparatorByYearLevel.java

package sandbox.course;

import java.util.Comparator;

class ComparatorByYearLevel implements Comparator

{

@Override

public int compare(Course c1, Course c2)

{

if(c1.getYearLevel() < c2.getYearLevel()) return -1;

else if(c1.getYearLevel() > c2.getYearLevel()) return 1;

else return 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!