CIT111: Java Session 2

Variables, operators, and basics

Session Overview

We gained some exposure to the core ways in which Java code "flows" and works with the Car Fob example. Now we want to step back a tad and make sure we have essentials down before expanding.

Learning Objectives

  1. Correctly create variable and class names that are descriptive and are not reserved or keywords.
  2. Interalize the idea of a java primitive and non-primitive type with the use of a method on the String class.
  3. Experience the concept of error types through the notion of type safety in variable names.
  4. Use an existing class in the Java library to gather input from the user for processing.
  5. Create a value converter using constants and variables and user input.
  6. Share a value converter object with peers.

Activity 1: The Java language essentials

All text that we write in a Java program is parsed by the compiler into categories of symbols that have meaning to the computer. These core categories are:

Activity 2: Gathering user input

Our goal is to retrieve a text value from the user and store it in a String type variable for processing by the computer and display to the user.

package essentials;

import java.util.Scanner;

public class GreetingMachine {

public static void main(String[] args) {
System.out.println("Please enter your name");
Scanner scan = new Scanner(System.in);
String name = scan.next();
System.out.println("Whoa, " + name + " What an interesting name card you have!");
} // Close main()
} // close class GreetingMachine

  1. Create a class file in a new project called GreetingMachine and paste in this code so that it compiles.
  2. Indent the code properly so lines that make up a block are grouped by indentation
  3. Change the greeting to something interesting and create a neat String art

Activity 3: Converters

Sudy this converter code

package essentials;

import java.util.Scanner;

/**
* Class which converts a value entered by the user into a new type of data
* @author delores
*/
public class Converter {

public static void main(String[] args){
// Basic prompt
System.out.println("Enter a number of days:");
// Create a scanner object that reads input
Scanner scan = new Scanner(System.in);
// now gather input and store it in the local variable days
int days = scan.nextInt();
// Send das to our converter method which will convert and display values
convertDaysToSeconds(days);

} // close main()

public static void convertDaysToSeconds(int days){
final int SECONDS_PER_HOUR = 60 * 60;
int seconds = days * SECONDS_PER_HOUR;
System.out.println(days + " days is the equivalent of " + seconds + " seconds");
} // close convertDaysToSeconds()

} // close Class

Now let's adjust this code to convert days to months, assuming a month has 30 days in it.

Practice and Extensions

  1. Create a converter that changes metric to English units using the correct operators.
  2. Create a converter between to interesting values in your life that are tradeoffs of one another, such as hours of sleep to plays of your favorite song. Or hours worked to milkshakes.
  3. Share that converter with others in the class
`