Unit 1 Primitives Notes

Notes

  • Primitative Types include
  • Boolean (true/false), one bit
  • Int (integer values), 2-3 bits
  • Double (Decimal values) 64 bits
  • Short, Byte, Floats, Char
  • Operators include
  • /+ is addition
  • /- is subtraction
  • / is division
  • is modulus (remainder)
  • /* is multiplication
  • ++ ex ++x is x=x+1
  • -- ex --x is x= x-1
  • += ex x+=3 is x=x+3
  • -=ex x-=3 is x=x-3
  • Compound assignment operators (+=. -=) can be used in place of regular assignment operators
  • ++ and -- are increase and decrease operators
public class CompOpsDemo {
    public static void main(String[] args) {
        int x = 6;
        x += 7;
        x -= 3;
        x *= 10;
        x /= 5;
        x %= 3;
        System.out.println("x = " + x);
    }
}
CompOpsDemo.main(null);
  • Casting and Ranges
  • Doubles and Integers can be converted to each other using (int) or (double)
  • When converting from doubles to integers, will round down
  • Integers are 4 bytes of data, can store between Integer.MAX_VALUE and Integer.MIN_VALUE
public class Cast {

    public static void main(String[] args) {
        double num = 10.5;
        int num2 = 100;
        int numInt = (int)num;
        double num2Double = (double)num2;

        System.out.println(num);
        System.out.println(num2);
        System.out.println(numInt);
        System.out.println(num2Double);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);


    }
}

Cast.main(null);
  • primitives - predefined, lowercase, "Primitives", can't call methods, has a value, has different sizes according to type
  • non-primitives - defined by you, uppercase, "Reference Types", can call methods, can be null, all the same size

Practice

import java.util.Scanner;
public class testScoreApp {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
 
 
       {
           // display operational messages
           System.out.println("Please enter test scores that range from 0 to 100.");
           System.out.println("To end the program enter 999.");
           System.out.println();  // print a blank line
 
           // initialize variables and create a Scanner object
            
           Scanner sc = new Scanner(System.in);
           String choice = "y";
 
           // get a series of test scores from the user
           while (!choice.equalsIgnoreCase("n"))
           {
               int scoreTotal = 0;
               int scoreCount = 0;
               int testScore = 0;
               System.out.println("Enter the number of test score to be entered: ");
               int numberOfTestScores = sc.nextInt();
 
               for (int i = 1; i <= numberOfTestScores; i++)
               {
                    // get the input from the user
                    System.out.print("Enter score " + i + ": ");
                    testScore = sc.nextInt();
 
                    // accumulate score count and score total
                    if (testScore <= 100)
                    {
                         scoreCount = scoreCount + 1;
                         scoreTotal = scoreTotal + testScore;
                     
                      
                    }
                    else if (testScore > 100) 
                 System.out.println("Invalid entry, not counted");
                     
 
 
                }
                double averageScore = scoreTotal / scoreCount;
                String message = "\n" +
                     "Score count:   " + scoreCount + "\n"
                   + "Score total:   " + scoreTotal + "\n"
                   + "Average score: " + averageScore + "\n";
                System.out.println(message);
                System.out.println();
                System.out.println("Enter more test scores? (y/n)");
                choice= sc.next();
          }
 
 
 
    // display the score count, score total, and average score
 
 
 
    }
}
    }

Unit 1 Primitives Homework

Submitted to Group 1 in a seperate repo

import java.util.Scanner;
import java.lang.Math;

public class GradeCalculator {

    private String category;
    private double current;
    private double percent;
    private double desired;
    private double test;

    private double percent_test;
    private double current_test;
    private int points_test;
    private int points_final;

    public double calculate() {
        this.enterCat();

        if (category.equals("separate")) {
            this.enterVals();
            test = (desired - (current * (1-percent/100))) / (percent/100);
        }

        else if (category.equals("tests")) {
            this.enterPoints();
            percent_test /= 100;
            int current_points = (int)current - (int)(percent_test) * points_test;
            test = ((desired - ((1-percent_test)*current_points)/percent_test) - points_test);
        }

        return test;
    }

    private void enterCat() {
        while (true) {
            Scanner dd = new Scanner(System.in);
            System.out.print("Enter final category (separate/tests): ");
            try {
                category = dd.nextLine();
                System.out.println(category);
                break;

            } catch (Exception e) { 
                System.out.println("Not a string, " + e);
            }
            dd.close();
        }
    }

    private void enterVals() {
        while (true) {
            Scanner dd2 = new Scanner(System.in);
            try {
                System.out.print("Current grade: ");
                current = dd2.nextDouble();
                System.out.println(current);

                System.out.print("Percentage of grade that is final: ");
                percent = dd2.nextDouble();
                System.out.println(percent);

                System.out.print("Desired grade: ");
                desired = dd2.nextDouble();
                System.out.println(desired);

                break;
            } catch (Exception e) { 
                System.out.println(current + "not a double, " + e);
            }
            dd2.close();
        }
    }

    private void enterPoints() {
        while (true) {
            Scanner dd3 = new Scanner(System.in);
            try {
                System.out.print("Current grade: ");
                current = dd3.nextDouble();
                System.out.println(current);

                System.out.print("Percentage of grade that is tests: ");
                percent = dd3.nextDouble();
                System.out.println(percent_test);
                
                System.out.print("Current percent in tests: ");
                current_test = dd3.nextDouble();
                System.out.println(current_test);
                
                System.out.print("Current amount of points in tests: ");
                points_test = dd3.nextInt();
                System.out.println(points_test);

                System.out.print("Amount of points in final: ");
                points_final = dd3.nextInt();
                System.out.println(points_final);

                System.out.print("Desired grade: ");
                desired = dd3.nextDouble();
                System.out.println(desired);

                break;
            } catch (Exception e) { // catch non-numerical input
                System.out.println(current + "not a double, " + e);
            }
            dd3.close();
        }
    }

    public static void main(String[] args) {    // main method
        GradeCalculator calc = new GradeCalculator();
        System.out.println("You need a " + String.format("%.2f", calc.calculate()) + " on the test");
    }
}

GradeCalculator.main(null);