在Java中使用课程和先决条件对象的递归。

huangapple go评论85阅读模式
英文:

Recursion in java with objects of courses and prerequisites

问题

import java.util.*;

public class Course {
    protected String courseNumber;
    protected String courseName;
    protected Course prerequisite;

    public Course() {
        courseNumber = courseName = "Unknown";
        prerequisite = null;
    }

    public Course(String cn, String num) {
        this.courseNumber = num;
        this.courseName = cn;
    }

    public String getCourseNumber() {
        return courseNumber;
    }

    public String getCourseName() {
        return courseName;
    }

    public Course getPreReq() {
        return prerequisite;
    }

    public void setCourseNumber(String courseNumber) {
        this.courseNumber = courseNumber;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setPreReq(Course pr) {
        prerequisite = pr;
    }
}

public class Prereqs {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);

        Course nineteen03 = new Course("Programming Fundamentals I", "ACS-1903");
        Course nineteen04 = new Course("Programming Fundamentals II", "ACS-1904");
        Course two47 = new Course("Data Structures and Algorithms", "ACS-2947");
        Course three47 = new Course("Algorithm Design", "ACS-3947");
        Course two09 = new Course("Internet Programming", "ACS-2909");
        Course three09 = new Course("Advanced Internet Programming", "ACS-3909");

        nineteen04.setPreReq(nineteen03);
        two47.setPreReq(nineteen04);
        three47.setPreReq(two47);
        two09.setPreReq(nineteen03);
        three09.setPreReq(nineteen03);

        System.out.println("Enter course number with the format: AAA-999");
        String input = kb.next();

        validate(input);
    }

    public static void course(Course nineteen04, Course nineteen03, Course two47, Course three47,
                              Course two09, Course three09, String input) {
        Course c1 = nineteen04.getPreReq();
        Course c2 = two47.getPreReq();
        Course c3 = three47.getPreReq();
        Course c4 = two09.getPreReq();
        Course c5 = three09.getPreReq();

        switch (input) {
            case "ACS-1904":
                System.out.println(nineteen04.getCourseName() + " " + nineteen04.getCourseNumber());
                System.out.println("preReq: " + c1.getCourseName() + " " + c1.getCourseNumber());
        }
    }

    public static String validate(String input) {
        String arg = input;

        boolean valid = arg.length() == 7;

        if (!valid) {
            throw new IllegalArgumentException("Not the correct format: AAA-999");
        }

        valid = arg.charAt(3) == '-';

        if (!valid) {
            throw new IllegalArgumentException("Not the correct format: AAA-999");
        }

        for (int i = 0; i < 3 && valid; i++) {
            valid = ((i == 3 && Character.isLetter(arg.charAt(i))));
        }

        for (int i = 3; i < 3 && valid; i++) {
            valid = ((i == 6 && Character.isDigit(arg.charAt(i))));
        }
        
        return arg;
    }
}
英文:

Good day, is there a more efficient way to do this problem with recursion than using a switch statement. In my courses class I have a recursive association of course and a prerequisite then a set the preReqs in the PreReqs class. How can I print out all of preReqs of a class when a user enter a class which has preReqs? Right now I am in the process of using a switch statement and printing each preReq individually but is there a better way to do this still using recursion?

An example out of this: If the user types that course, all of the preReqs will print out too.

ACS-3947 Algorithm Design
prereq: ACS-2947 Data Structures and Algorithms
ACS-2947 Data Structures and Algorithms
prereq: ACS-1904 Programming Fundamentals II
ACS-1904 Programming Fundamentals II
prereq: ACS-1903 Programming Fundamentals I
ACS-1903 Programming Fundamentals I
no prereq

Course class:

import java.util.*;
public class Course
{  
protected String courseNumber; 
protected String courseName;
protected Course prerequisite; 
public Course(){
courseNumber = courseName =  &quot;Unknown&quot;; 
prerequisite= null; 
}
public Course (String cn, String num){
this.courseNumber=num;
this.courseName=cn; 
}
public String getCourseNumber(){
return courseNumber;
}
public String getCourseName(){
return courseName;
}
public Course getPreReq(){
return prerequisite;
}
public void setCourseNumber(String courseNumber){
this.courseNumber=courseNumber;
}
public void setCourseName(String courseName){
this.courseName=courseName;
}
public void setPreReq(Course pr){
prerequisite =pr; 
}
}

PreReq class:

import java.util.*;
import java.io.*;
public class Prereqs 
{
public static void main (String [] args){
Scanner kb = new Scanner (System.in);
Course nineteen03 = new Course (&quot;Programming Fundamentals I&quot;,&quot;ACS-1903&quot;);
Course nineteen04 = new Course (&quot;Programming Fundamentals II&quot;,&quot; ACS-1904&quot;);
Course two47 = new Course (&quot;Data Structures and Algorithms&quot;,&quot;ACS-2947 &quot;); 
Course three47 = new Course (&quot;Algorithm Design&quot;,&quot;ACS-3947&quot;);
Course two09 = new Course (&quot;Internet Programming&quot;,&quot;ACS-2909&quot;);
Course three09 = new Course (&quot;Advanced Internet Programming &quot;,&quot;ACS-3909&quot;);
nineteen04.setPreReq(nineteen03);
two47.setPreReq(nineteen04);
three47.setPreReq(two47);
two09.setPreReq(nineteen03);
three09.setPreReq(nineteen03); 
System.out.println(&quot;Enter course number with the format: AAA-999&quot;);
String input = kb.next(); 
validate(input);
}
public static void course(Course nineteen04, Course nineteen03,Course two47, Course three47, Course two09, Course three09, String input  ){
Course c1 = nineteen04.getPreReq();
Course c2 = two47.getPreReq();
Course c3 = three47.getPreReq(); 
Course c4 = two09.getPreReq();
Course c5 = three09.getPreReq();
switch (input){
case &quot;ACS-1904&quot;:
System.out.println(nineteen04.getCourseName()+&quot; &quot;+nineteen04.getCourseNumber());
System.out.println(&quot;preReq: &quot; + c1.getCourseName()+ &quot; &quot;+ c1.getCourseNumber());
}
}
public static String validate (String input)
{   
String arg = input;
boolean valid = arg.length()==7;
if (!valid){
throw new IllegalArgumentException(&quot;Not the correct format: AAA-999&quot;);           
}
valid = arg.charAt(3) == &#39;-&#39;;  
if(!valid) {
throw new IllegalArgumentException(&quot;Not the correct format: AAA-999&quot;);
}
for(int i=0; i &lt; 3 &amp;&amp; valid; i++){
valid = ((i == 3 &amp;&amp; Character.isLetter(arg.charAt(i))));
}
for(int i=3; i &lt; 3 &amp;&amp; valid; i++){
valid = ((i==6 &amp;&amp; Character.isDigit(arg.charAt(i))));
}
return arg;
}
}

答案1

得分: 1

public class Course {
    protected String courseNumber;
    protected String courseName;
    protected Course prerequisite;

    public Course(){
        courseNumber = courseName = "Unknown";
        prerequisite= null;
    }

    public Course (String cn, String num){
        this.courseNumber=num;
        this.courseName=cn;
    }

    public String getCourseNumber(){
        return courseNumber;
    }

    public String getCourseName(){
        return courseName;
    }

    public Course getPreReq(){
        return prerequisite;
    }

    public void setCourseNumber(String courseNumber){
        this.courseNumber=courseNumber;
    }

    public void setCourseName(String courseName){
        this.courseName=courseName;
    }

    public void setPreReq(Course pr){
        prerequisite =pr;
    }

    public String toString() {
        return courseNumber + " " + courseName;
    }

    private static void requirements(Course c) {
        if (c == null) {
            return;
        }
        else {
            System.out.println(c);
            requirements(c.getPreReq());
        }
    }

    public static void main(String[] args) {
        Course nineteen03 = new Course ("Programming Fundamentals I","ACS-1903");
        Course nineteen04 = new Course ("Programming Fundamentals II"," ACS-1904");
        Course two47 = new Course ("Data Structures and Algorithms","ACS-2947 "); 
        Course three47 = new Course ("Algorithm Design","ACS-3947");
        Course two09 = new Course ("Internet Programming","ACS-2909");
        Course three09 = new Course ("Advanced Internet Programming ","ACS-3909");

        nineteen04.setPreReq(nineteen03);
        two47.setPreReq(nineteen04);
        three47.setPreReq(two47);
        two09.setPreReq(nineteen03);
        three09.setPreReq(nineteen03);
        
        requirements(three09);
    }
}

Running the above code displays the following:

ACS-3909 Advanced Internet Programming 
ACS-1903 Programming Fundamentals I
英文:

A recursive method needs to contain a condition which terminates the recursion. Your list of courses and their prerequisites remind me of a linked list where each course points to its prerequisite. The list terminates when we reach a course that has no prerequisite. The below code is your Course class with the addition of a main method (imported from your Prereqs class) and the recursive method which I named requirements(). I also added method toString() to make the display of the list of courses and their prerequisites more "human readable". You can experiment by changing the course passed to the initial invocation of method requirements().

public class Course {
    protected String courseNumber;
    protected String courseName;
    protected Course prerequisite;

    public Course(){
        courseNumber = courseName =  &quot;Unknown&quot;;
        prerequisite= null;
    }

    public Course (String cn, String num){
        this.courseNumber=num;
        this.courseName=cn;
    }

    public String getCourseNumber(){
        return courseNumber;
    }

    public String getCourseName(){
        return courseName;
    }

    public Course getPreReq(){
        return prerequisite;
    }

    public void setCourseNumber(String courseNumber){
        this.courseNumber=courseNumber;
    }

    public void setCourseName(String courseName){
        this.courseName=courseName;
    }

    public void setPreReq(Course pr){
        prerequisite =pr;
    }

    public String toString() {
        return courseNumber + &quot; &quot; + courseName;
    }

    private static void requirements(Course c) {
        if (c == null) {
            return;
        }
        else {
            System.out.println(c);
            requirements(c.getPreReq());
        }
    }

    public static void main(String[] args) {
        Course nineteen03 = new Course (&quot;Programming Fundamentals I&quot;,&quot;ACS-1903&quot;);
        Course nineteen04 = new Course (&quot;Programming Fundamentals II&quot;,&quot; ACS-1904&quot;);
        Course two47 = new Course (&quot;Data Structures and Algorithms&quot;,&quot;ACS-2947 &quot;); 
        Course three47 = new Course (&quot;Algorithm Design&quot;,&quot;ACS-3947&quot;);
        Course two09 = new Course (&quot;Internet Programming&quot;,&quot;ACS-2909&quot;);
        Course three09 = new Course (&quot;Advanced Internet Programming &quot;,&quot;ACS-3909&quot;);

        nineteen04.setPreReq(nineteen03);
        two47.setPreReq(nineteen04);
        three47.setPreReq(two47);
        two09.setPreReq(nineteen03);
        three09.setPreReq(nineteen03);
        
        requirements(three09);
    }
}

Running the above code displays the following:

ACS-3909 Advanced Internet Programming 
ACS-1903 Programming Fundamentals I

huangapple
  • 本文由 发表于 2020年4月7日 01:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61065850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定