Skip to content

marcnholley/java-tutorial

Repository files navigation

java-programming-level-1-final-project

Final project for Java Programming: Level I

My final project is a series of simple programs that build on each other adding new elements with each successive one. I started off by building a simple program to highlight control structures. The first program titled ControlStructures consists of one integer variable named numInput, a scanner object name scnr, a screen prompt to ask for the user input, the variable numInput’s value being assigned by the scanner object scnr’s next integer input, one if–else conditional statement, and two possible conditional outcomes. How the program works is the user is prompted to enter an integer that is even or odd. The scanner records the users input and assigns its value to the variable. The if- else conditional statement will determine if the variable is even or odd by dividing it in two and comparing the remainder with the logical operator to see if its value is the equivalent to zero. If the remainder of the divided variable is zero, the program will print the output “Your number is even.”, if it is not the program will print out “Your number is odd.”

The second program uses two program classes named Classes and Classes2. It consists of everything from the first program ControlStructures which is in Classes but adds a new class object from Classes2 called class1 and a method call of the class1 object from Classes2. Classes2 extends Classes and consists of a method which uses a pass by reference variable which is an integer named numInput and is the value of the numInput variable from Classes, and one if-else conditional statement that determines whether the pass by reference variable is a positive or negative number. The Classes class program works the exact same as the first program ControlStructures, except after it has executed all of its original code the program makes a method call of the class1 object to Classes2 named positiveOrNegative with the pass by reference numInput variable. The method uses the if-else conditional statement to determine if the variable is great than zero, if it is the program outputs “Your number is positive.” If it isn’t it outputs “Your number is negative.” as well as the output from Classes declaring it either and even or odd number.

The third program is named ErrorHandling. It consolidates the exact code from both Classes and Classes2 and adds an imported java utility InputMismatchException, a try statement before the prompt for user input to the variable numInput, a new if conditional statement that must meet two logical operator comparisons and if not throws a InputMismatchException exception and catch statement which catches the exception thrown from the throw statement and outputs a couple of error messages. The ErrorHandling program starts off differently than all of the other programs that have come before it. It beings with a try statement and then goes in to the prompt for user input variable. Then the variable is passed through a new if conditional statement. Again, it divides the variable by two and compares the remainder using two logical operators that must both be met to throw an exception. If the variable numInput is divided by two and the remainder is not the equivalent of zero or one, the InputMichmatchException is thrown. In the case of the thrown exception, the code is thrown to the catch statement where the program outputs the error messages “Invalid input!” “Must be an integer!” If the exception is not thrown the code executes in the same order as in the previous program.

The fourth program is named MethodsAndRecursion. It consists of the exact same code with the addition of the a new method called getNumber(). The new method contains the variable numInput, the scanner constructor, the try statement, user input prompt, if conditional statement that determines if the input throws an exception, throw exception statement, if-else conditional to determine if the variable is even or odd, the method call with the pass by reference variable, the catch exception with error messages, and a getNumber() recursive call. The code works the same as before, except this time if an exception is thrown, the catch statement causes the program to print the error messages and make a recursive call to the getNumber() method until the if-else conditional statements are met and the program completes, telling the user whether their choice is even or odd and positive or negative.

The fifth program is named ArrayProcessing. In this program the code is the exact same from previous program except for two added string objects named types. One which consists of even, odd, or error which is in the getNumber() method and the other with positive and negative in the pass by reference method positiveOrNegative(). The code executes exactly the same as the previous program, but when the output messages are printed, they make a call to the specific indexes of the strings contents.

Releases

No releases published

Packages

No packages published

Languages