Learning Objectives

System class methods print output to the console.

  • String literals
  • Primitive Data Types
  • Declaring variables to different data types
  • Use of arithmetic expressions in a program
  • Data stored in variables
  • Assignment Statements

String Literals

  • Declare a class: public class "name"
  • Use curly brackets in order to define where a class begins and where it ends
  • Put methods (at least one) inside of the curly brackets representing the class
  • Main methods: public static void main(String[] args)
  • Another curly bracket to define where the method begins and ends -- indented this time
  • System.out.println (""); -- lets us output text to our console, must end with a semicolon
public class Program {
    
    public static void main(String[] args) 
    {
        System.out.println("My name is Brian");
    }
}

Program.main(null)
My name is Brian

Declaring and Initializing Variables

  • Variables defined within a method are local variables
  • "Int" can hold any whole number
  • You need an initial value in order to change it or increment it
  • No quotation mark around variable because this will output the value stored by variable
public class Variables{
    public static void main(String[] args)
    {
        int x;
        x = 90;
        x = 5;
        x = x + 1;
        int Score = 90;

        System.out.println("The score is: "+ Score);
    }
}
Variables.main(null)
The score is: 90

8 Primitive Data Types

  • Byte: minimum value of -128 and maximum value of 127 (inclusive). It is useful for saving memory in large arrays
  • Short: Minimum value of -32,768 and maximum value of 32,767. Same purpose as Byte
  • Int: Any integer or whole number
  • Long: Greater range than int
  • Float: floating point numbers that tend to have decimals
  • Double: not good for precise data
  • Boolean: logic that evaluates whether a condition is true or false
  • Char
public class PrimitiveDataTypes
{
    public static void main(String[] args)
    {
        int a = 5;
        double b = 5.0;
        b = 3; // only works for double
        a = (int) 5.999; // casting cuts off everything after the decimal
        System.out.println(a);

        boolean c = true;
        c = false;
        System.out.println(c);
    }
}
PrimitiveDataTypes.main(null)
5
false

Arithmetic Operations

public class Operators 
{
    public static void main(String[] args)
    {
        int x;
        x = 5;

        System.out.println(5+2); 
        System.out.println(5-2);
        System.out.println(5*5);
        System.out.println(5/5);
        
        x = 3*3;
        x = 3 * x;

        System.out.println(5 % 3); // 5 mod 3 finds the remainder when 5 is divided by 3

    }
}
Operators.main(null)
7
3
25
1
2

2013 FRQ 1

Part 1

public DownloadInfo getDownloadInfo(String title) { // define the class
    for (DownloadInfo info : downloadList){
    if (info.getTitle().equals(title)){
     return info;
     }
    }
    return null; // method inside the class
    }

Part 2

public void updateDownloads(List<String> titles) {
    for (String title : titles) {
    DownloadInfo foundInfo = getDownloadInfo(title); // calls method
    if (foundInfo == null){
    downloadList.add(new DownloadInfo(title)); // makes new object
    }
    else {
    foundInfo.incrementTimesDownloaded();
    }
    }
   }