import java.util.Scanner; /** * Week 4 lab solution, includes all computation and input validation, * but not loops to get good input data. * * Version 1: uses various Scanner methods to get input * * Violates checkstyle cyclomatic complexity because of so many * decision statements in one method! */ public class Courses1 { /** * Main method, start of execution. * @param args not used */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int majorCode, // student's major department departNum, // class department courseNum, // class number indexW, indexS, indexH; // indices of 'W', 'S' and 'H' boolean elective = true; // not any requirements String designators; // department designators double course; // full course identifier double credits; // amount of credits final int math = 110; final int ams = 550; final int credmin = 3; final int maxnum = 999; final float maxcreds = 4.5f; final int factor = 5; final int thous = 1000; final int upper = 300; boolean valid = true; // input data is good // get student's major department System.out.println("Enter major department code: "); majorCode = keyboard.nextInt(); // input // get course data System.out.println("Enter course data (dpt.num credits HSEQNW): "); course = keyboard.nextDouble(); // course number input ###.### format credits = keyboard.nextDouble(); // amount of credits designators = keyboard.next(); // academic designators designators = designators.toUpperCase(); // for consistency // department part of course departNum = (int) course; // course number part of course courseNum = (int) (course * thous) % thous; // input validity checks if (departNum < 1 || departNum > maxnum || courseNum < 1 || courseNum > maxnum || majorCode < 1 || majorCode > maxnum || (int) (credits * 2 * factor) % factor != 0 || credits < 1 || credits > maxcreds) { valid = false; } // preview: need loop to check area designators validity for (int i = 0; i < designators.length(); i++) { if (("HSEQNW").indexOf(designators.charAt(i)) < 0) { valid = false; } } if (!valid) { System.out.println("invalid input, exitting program!"); } else { // search for 'H' in designators indexH = designators.indexOf('H'); // search for 'W' in designators indexW = designators.indexOf('W'); // search for 'S' in desginators indexS = designators.indexOf('S'); //copy double course code into string String courseStr = course + ""; //output user's data in parsed form final int dotPos = 3; System.out.println("Major code: " + majorCode); //substring methods are used to access parts of the course code System.out.println("Department #: " + courseStr.substring(0, dotPos)); System.out.println("Course #: " + courseStr.substring(dotPos + 1)); System.out.println("Credits: " + credits); System.out.println("Areas: " + designators); // if class department is 110 or 550... if (departNum == math | departNum == ams) { // the class counts for math requirement elective = false; // if any of these apply, elective becomes false System.out.println("Course can be counted towards your " + "math requirement."); } // if there is a W and the class is at least 3 credits if (indexW != -1 && credits >= credmin) { // the clss counts for writing requirement System.out.println("Course can be counted towards your " + "writing requirement."); elective = false; // if any of these apply, elective becomes false } // if there is a H or S designator and the class is at least 3 credits if ((indexH != -1 || indexS != -1) && credits >= credmin) { // the class counts for HS System.out.println("Course can be counted towards your " + "HS requirement."); elective = false; // if any of these apply, elective becomes false } // if the class is in the same class as the student's major... if (majorCode == departNum) { elective = false; // and the class number is < 300 if (courseNum < upper) { // the class satisfies a lower level major req. System.out.println("Course can be counted towards your " + "lower level major requirement."); } else { // the class satisfies an upper level major req System.out.println("Course can be counted towards your " + "upper level major requirement."); } } // if elective is still true if (elective) { // the class counts as an elective System.out.println("Course can be counted as " + "elective credit only."); } } } }