Lucky Cat Tutoring Services


CIT 111 Java 101 week 2
August 25, 2009, 4:36 pm
Filed under: Class Notes, Java | Tags: , , ,

Time for more notes from D Smith’s CIT 111 class at CCAC.

A quick review:
–Classs can stack
–You can only have 1 main class
–You can have as many “Methods” in your class as you want
–All you need to get a program to run at all is a basic skeleton:

/*This is where comments go
 *
 *
 *
 */
public class nameOfProgram
{
        public static void main(String[] args)
        {

        }
}

 

Lets make a little program to draw a star pattern using last week’s lesson:

public class StarProgram
 { public static void main(String []args)
  {
   System.out.println(”    *    “);
   System.out.println(“*       *”);
   System.out.println(”    *   “);
   System.out.println(”    *    “);
   System.out.println(”   ***   “);
   System.out.println(”  *****  “);
   System.out.println(” ******* “);
   System.out.println(”  *****  “);
   System.out.println(”   ***   “);
   System.out.println(”    *    “);
  }
 } // ends the class and program

This displays a little star pattern like old school “Hi I’m drawing with emoticons” postings from the dark ages of the internet.  Don’t like how it looks? No need to delete a line of code:

Just turn whatever line that bothers you into a comment by dropping a “//” in front; great for debugging.

————————-

Learning about initializing variables

1)Initizalizing
–if you plan to use a variable you need to declare it

–Variable can just be declared: ex int hours

–Assign set amount: ex int hours = 40;

DATA TYPES
int (INTEGERS) ex 7, 27, whole numbers
—Byte: 1 byte of memory
—Short: 2 bytes
—Long: 8 bytes. All LONGS must have the suffix “L” at the end or it will be output as an integer and not a long
double (FLOATING POINT) ex 7.1, 27.3 decimals
EXAMPLE:
public class IntegerVariables
{
public static void main(String[] args)
{
int checking; // Declare an int variable named checking.
byte miles; // Declare a byte variable named miles.
short minutes; // Declare a short variable named minutes.
long days; // Declare a long variable named days.

//assignments to the data types, not redeclared
checking = -20;
miles = 105;
minutes = 120;
days = 185000L; //make sure it stays long
// int years = days / 365; mixed data types don’t work
int years = (int)days / 365; //years must be long; can’t fit 8 bytes of stuff in 4 byte box. Use explicite casting

Within integers and floating points there are different types but that will be covered in more advanced lessons

 

2) Displaying
–a) Special Sequence Characters:
——TAB = \t
——RETURN =\n

——!, ?, etc =\!, \? \etc
——DISPLAY backslash: double up the \\. \\ = \

————————————————————-

Example of a basic computation problem:

import java.util.Scanner; // Needed for the Scanner class

/*
This program correctly read numeric and string input.
*/

public class LabAssign2
{
public static void main(String[] args)
{
String mpg;
int miles; // To hold miles
double gas; // To hold the gas

// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);

// Get the user’s miles.
System.out.print(“Enter the Miles Drive–> “);
miles = keyboard.nextInt();

// Get the user’s gas
System.out.print(“Enter the gallons of fuel used–> “);
gas = keyboard.nextDouble();
// mpg = miles / (int)gas;

// Consume the remaining newline.
keyboard.nextLine();

// Display the information back to the user.
System.out.println(“\nThe miles-per-gallon based on the data entered is: ” +miles / +gas);
}
}

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: