logo

APSU Notes


Refresher Materials

Chapter 1 - Introduction to Computers and Java

Write, compile, and run a simple Java program using Netbeans.

Files

ADD 1010 LINKS!!!

Videos

Chapter 2 - Variables and Expressions

Use input, output, and assignment statements to write a Java program. Compute values using floating point numbers. Use named constants to represent values that do not change.

Files

Videos

Chapter 3 - Strings, I/O, and Programming Style

Represent text data with constants and variables of type String. Process text using String class methods. Read text input. Display text output. Use standard Java documentation and style.

Files

CSCI 1011 Lab Assignment 3.docx

Variables.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package csci1011.l3;

import java.util.Scanner;
/**
 * Some information on Variables in Java
 * @author {Dr.G.}
 */
public class Variables {
    // Declare constants here
    static double PII = 3.14;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // if you are prompting the user then you need a keyboard variable.
         // Declare Variables
        Scanner keyboard = new Scanner(System.in);

        // You do not have have to prompt the user to create variables
        // However you should initialize those variables
        int len = 0;
        // declaring multiple variables of the same type
        double radius,circumference;
        // initializing those variables.
        radius = circumference = 0.0;

        // prompt the user to enter a String
        System.out.println("Please enter multiple words on one line");
        // Using next should only get the first word
        String test1 = keyboard.next();
        System.out.println ("you entered " + test1);
        //Clear Buffer
        test1 = keyboard.nextLine();

        // To get multiple words use nextLine
        System.out.println("Please enter multiple words on one line");
        // Using next should only get the first word
        //Since test1 was declared above you do not have to redeclare it
        test1 = keyboard.nextLine();
        System.out.println ("you entered " + test1);

        // List of built in String methods https://www.w3schools.com/java/java_ref_string.asp
        // use one of the built in string methods
        len = test1.length();
        System.out.println ("The length of string is : " + len);

        // ok using the radius and circumference variables and the CONSTANT
        System.out.println ("using the radius and circumference variables");

        System.out.println("Please enter a radius");
        radius = keyboard.nextDouble();

        circumference = 2 * PII * radius;
        System.out.printf("The circumference of the circle = %8.2f %n", circumference);


    }

}

Videos

Chapter 4 - Flow of Control - Branching

Alter the flow of the control in a program using branching statements. Construct Boolean expressions using comparison and logical operators.

Files

BMI.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package csci1011.l4;
import java.util.Scanner;

public class BMI
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int pounds = 0;
        int feet = 0 ;
        int inches = 0 ;
        double heightMeters = 0.0;
        double mass = 0.0 ;
        double BMI= 0.0;

        System.out.println("Enter your weight in pounds.");
        pounds = keyboard.nextInt();
        System.out.println("Enter your height in feet followed");
        System.out.println("by a space then additional inches.");
        feet = keyboard.nextInt();
        inches = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Please enter your name");
        String sName = keyboard.nextLine();

        heightMeters = ((feet * 12) + inches) * 0.0254;
        mass = (pounds / 2.2);
        BMI = mass / (heightMeters * heightMeters);

        System.out.println(sName + " Your BMI is " + BMI);
        System.out.print("Your risk category is ");
        if (BMI < 18.5){
                System.out.println("Underweight.");
        } else if (BMI < 25) {
                System.out.println("Normal weight.");
        } else if (BMI < 30) {
            System.out.println("Overweight.");
        } else {
            System.out.println("Obese.");
        }
    }
}

Grader.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package csci1011.l4;
import java.util.Scanner;

public class Grader
{
    public static void main(String[] args)
    {
        int score;
        char grade;

        System.out.println("Enter your score: ");
        Scanner keyboard = new Scanner(System.in);
        score = keyboard.nextInt( );

        if (score >= 90)
            grade = 'A';
        else if (score >= 80)
            grade = 'B';
        else if (score >= 70)
            grade = 'C';
        else if (score >= 60)
            grade = 'D';
        else
            grade = 'F';

        System.out.println("Score = " + score);
        System.out.println("Grade = " + grade);
    }
}

Videos

Chapter 5 - Flow of Control - More Branching and Loops

Develop a menu-based interface using control structures. Use switch statements with integers, characters, and strings. Write code that repeatedly performs actions using while and do-while loops.

Files

CSCI 1010 Lab Assignment 5.docx

loops.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package csci1011.l5;
import java.util.Scanner;
/**
 * This application will provide information on how to write while, do while and for loops
 * @author {Dr.G.}
 */
public class loops {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // local variables
        Scanner keyboard = new Scanner(System.in);
        int i, j;
        i = j = 0;

        // use for loops when the number of times a loop must execute
        // redeclaring i in two place is bad programming practice, however, it will work because
        // of scope. The scope of a variable is where that variable can be accessed in a program.
        // Variables declared at the start of a program can be accessed anywhere in the program.
        // variables declared inside braces are only known within those braces.
        // Prompt the user for to enter a integer value.
        System.out.println("Please enter number :");
        j = keyboard.nextInt();

        // it is best to clear the buffer after getting a number from the user.
        keyboard.nextLine();
        System.out.println ("The for loop");
        // output a message to the user while looping
        for (i = 0 ; i < j; i++) {
            System.out.println ("The value of i = " + i);
        }
        System.out.println ();
        System.out.println ();
        // Use a while loop to execute a loop while a condition is true.
        // if the condition is false the program will jump to the next statement after the loop
        System.out.println ("The While loop");
        while (i > 0) {
            // you have to decrement/ or increment the loop variable otherwise you will enter an endless loop
            System.out.println ("The value of i = " + i);
            i--; // this will decrement i
        }
        System.out.println ();
        System.out.println ();
        // Use a do while loop if you want to execute the code in the body of the loop at least once
        String response;
        System.out.println ("The Do While loop");
        do {
            System.out.println ("The value of i = " + i);
            System.out.println("WOuld you like to loop again ? (Y/N)");
            response = keyboard.nextLine();
            if (response.equalsIgnoreCase("y")) {
                i++;
            }

        } while (response.equalsIgnoreCase("y"));
    }

}

switchstatements.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package csci1011.l5;
import java.util.Scanner;
/**
 *
 * @author {Dr.G.}
 */
public class switchstatements {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Local variables
        Scanner keyboard = new Scanner(System.in);
        char grade;
        String response;
        // switch statement will only work with simple data types (integers, characters)

        /* so using a grade in reverse
            if a user enters A then out 100
            if the user enters B then output 80
            if the user enters C then output 70
            If the user enters D output 60
            default case output try again
        */
        System.out.println("Please enter a character A, B, C, D, ");
        response = keyboard.next();
        grade = response.charAt(0);

        switch (grade) {
            case 'a':
            case 'A': System.out.println("Grade equals 100");
                      break;
            case 'b':
            case 'B': System.out.println("Grade equals 80");
                      break;
            case 'c':
            case 'C': System.out.println("Grade equals 70");
                      break;
            case 'd':
            case 'D': System.out.println("Grade equals 60");
                      break;
            default:
                      System.out.println("Try again!");
        }


        // clear buffer
        keyboard.nextLine();
        // prompt the user to enter 3 pieces of information separated by spaces
        int num1, num2;
        System.out.println("Please enter a number space character space and another number");
        num1 = keyboard.nextInt();
        response = keyboard.next();
        num2 = keyboard.nextInt();

        char test = response.charAt(0);

        // output the values to the user
        System.out.printf("You entered %d  %c  %d%n%n", num1, test, num2);


        // Switch statement using integers
        System.out.println("Please enter a number less than 4");
        num1 = keyboard.nextInt();

        switch (num1){
            case 1: System.out.println("You entered 1");
                    break;
            case 2:
            case 3:System.out.println("You entered 2 or 3");
                   break;
            case 4:System.out.println("You entered 4");
                    break;
            default :  System.out.println("You entered " +num1);

        }
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package csci1011.l5;
import java.util.Scanner;
/**
 *
 * @author {Dr.G.}
 */
public class menu {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         // Local variables
        Scanner keyboard = new Scanner(System.in);
        int user = 0;
        String response;

        do {
            // output a ment to the user
            System.out.println("Choose one of the following options: ");
            System.out.println("1. Perform an arithmetic operation");
            System.out.println("2. Apply a function");
            System.out.println("3. Calculate a factorial");
            System.out.println("4. Exit the program");

            System.out.println("Please enter a number between 1 and 4");
            // get user response
            user = keyboard.nextInt();

            // switch statement
            switch (user) {

                case 1: // do something until break
                        System.out.println ("you entered 1");
                        // you can have case statements inside case statements
                        int num1, num2;

                        // clear buffer
                        keyboard.nextLine();
                        // prompt the user to enter 3 pieces of information separated by spaces

                        System.out.println("Please enter a number space character space and another number");
                        num1 = keyboard.nextInt();
                        response = keyboard.next();
                        num2 = keyboard.nextInt();

                        char test = response.charAt(0);

                        // output the values to the user
                        System.out.printf("You entered %d  %c  %d%n%n", num1, test, num2);

                        break;
                case 2: // do something until break
                        System.out.println ("you entered 2");
                        break;
                case 3: // do something until break
                        System.out.println ("you entered 3");
                        break;
                case 4: // do something until break
                        System.out.println ("you entered 4");
                        break;
                default: System.out.println ("you entered " + user);
            }





        } while (user != 4);
        System.out.println ("Ending the program");
    }

}

Videos

Chapter 7 - Defining Classes & Methods I

Video describing how to make Classes and Methods

Videos

Chapter 11 - Arrays

Represent collections of data using arrays. Solve computational problems using loops and arrays. Construct classes and methods that operate on arrays.

Files

Driver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package csci1011.sales;
import  java.util.Scanner;
/**
 *
 * @author {Dr.G.}
 */
public class Driver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner kb = new Scanner (System.in);
        String response;
        do {
            SalesData ASaleperson = createSalesPerson();
            System.out.println("Total Sales : " + ASaleperson.getTotal());
            System.out.println("Minimum Sale : " + ASaleperson.getMIN());
            System.out.println("Max Sale : " + ASaleperson.getMax());
            System.out.println("Would you like to enter another sales person? Yes or No");
            response = kb.nextLine();
        } while (response.equalsIgnoreCase("yes") );
    }
    private static SalesData createSalesPerson(){
        Scanner kb = new Scanner (System.in);
        String name, div;
        int num = 0;
        System.out.println("Please Enter your full name: ");
        name = kb.nextLine();
        System.out.println("Please enter your division: ");
        div = kb.nextLine();
        System.out.println("Enter the number of sales for this month: ");
        num = kb.nextInt();

        // clear buffer
        kb.nextLine();
        SalesData aSalesPerson = new SalesData(name, div, num);
        aSalesPerson.readSales();
        return aSalesPerson;
    }

}

SalesData.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package csci1011.sales;
import  java.util.Scanner;
/**
 *
 * @author {Dr.G.}
 */
public class SalesData {
    // constant used finding min and max
    public static double MAX = -1.0;
    public static double MIN = 1000000.00;

    // Private data members
    private String Name;
    private String Division;
    private int NumSales;
    private double [] Sales;

    // pass in salesperson, division and number of sales
    public SalesData(String Aname, String aDiv, int newSales){
        this.Name = Aname;
        this.Division = aDiv;
        this.NumSales = newSales;
        Sales = new double[newSales];
    }
    public void readSales(){
        Scanner keyboard = new Scanner (System.in);

        for (int i = 0; i < this.NumSales; i++) {
            System.out.println("Enter next sale : ");
            Sales[i] = keyboard.nextDouble();
        }
    }
    public double getTotal(){
        double total = 0.0;
        for (int i =0; i < this.NumSales; i++) {
            total += Sales[i];
        }
        return total;
    }
    public double getMIN (){
        double min = MIN;
        for (int i =0; i < this.NumSales; i++) {
            if (Sales[i] < min)
                min = Sales[i];
        }
        return min;
    }
    public double getMax (){
        double max = MAX;
        for (int i =0; i < this.NumSales; i++) {
            if (Sales[i] > max)
                max = Sales[i];
        }
        return max;
    }
}

Videos

Chapter 12 - Exceptions and Files

Implement code that handles exceptions. Read and process data from text files. Write output to text files.

Files

lab12_example.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package csci2011.l10;
import java.util.Scanner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
/**
 *
 * @author {Dr.G.}
 */
public class lab10_example {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Scanner keyboard = new Scanner(System.in);
        String filename;
        // prompt for file name
        System.out.println ("Enter the name of a file :");
        filename = keyboard.nextLine();
        int choice;
        do {
            displayMenu();
            choice = getChoice();
            switch(choice) {
                 case 1:

                    System.out.println();
                    PrintWriter outFile = null;
                    try {
                        // use a boolean to let PrintWriter know if it the file exist(false)
                        outFile = openFileForWriting(filename);
                        System.out.println("File was opened");
                        //writelines_to_File (outFile);
                        String name;
                        System.out.println ("Enter names in File");
                        int num = 0;
                        System.out.println ("How many names are you entering: ");
                        num = keyboard.nextInt();
                        //clear buffer
                        keyboard.nextLine();
                        for (int i = 0; i < num; i++){
                            // prompt for a name
                            System.out.println ("Enter next name: ");
                            name = keyboard.nextLine();
                            outFile.println(name);
                        }
                    }
                    catch (FileNotFoundException e) {
                        System.out.println("The file was not found!");
                        System.exit(1);
                    }
                    finally{ // you need to make sure you close the file
                        if (outFile != null) {
                            outFile.close();
                        }
                    }

                    break;
                 case 2:
                     // Read data from file
                     Scanner inputFile = null;
                     System.out.println("The file " + filename + " contains these names: ");
                     try{
                         inputFile = new Scanner(new File(filename));
                     }
                     catch (FileNotFoundException e) {
                        System.out.println("The file was not found!");
                        System.exit(1);

                     }
                     // output the contents to the screen
                     while (inputFile.hasNextLine()){
                         String line = inputFile.nextLine();
                         System.out.println(line);
                     }

                     System.out.println();
                     // close file
                     inputFile.close();
                     break;
                 case 3:
                     PrintWriter appendFile = null;
                     try {
                     // use a boolean to let PrintWriter know if it the file exist(false)
                        appendFile = openFileForAppending(filename);
                        System.out.println("File was opened");
                        //writelines_to_File (outFile);
                        String name;
                        System.out.println ("Enter names in File");
                        int num = 0;
                        System.out.println ("How many names are you entering: ");
                        num = keyboard.nextInt();
                        //clear buffer
                        keyboard.nextLine();
                        for (int i = 0; i < num; i++){
                            // prompt for a name
                            System.out.println ("Enter next name: ");
                            name = keyboard.nextLine();
                            appendFile.println(name);
                        }
                    }
                    catch (FileNotFoundException e) {
                        System.out.println("The file was not found!");
                        System.exit(1);
                    }
                    finally{ // you need to make sure you close the file
                        if (appendFile != null) {
                            appendFile.close();
                        }
                    }
                     break;
                 case 4:
                    System.out.println("Thank you for using file manager!");
                    System.out.println();
                     break;
            }
           } while (choice != 4);
    }
     /**
     * Display the menu for the program
     */
    private static void displayMenu() {
        System.out.println("Choose one of the following:");
        System.out.println("1. Open File for output");
        System.out.println("2. Display Contents of File");
        System.out.println("3. Append Data to file");
        System.out.println("4. Exit the program");
    }

    /**
     * Get the users menu item choice
     * @return the menu item the user chose
     */
    private static int getChoice() {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();
        keyboard.nextLine();
        System.out.println();
        return choice;
    }
    public static PrintWriter openFileForWriting (String filename) throws FileNotFoundException {

            // open file for output
            PrintWriter outFile = new PrintWriter (filename);
            return outFile;

    }
   public static PrintWriter openFileForAppending (String filename) throws FileNotFoundException {

            // open file for output
            PrintWriter outFile = new PrintWriter (new FileOutputStream(filename, true));
            return outFile;

    }
}

Videos