Documente Academic
Documente Profesional
Documente Cultură
Java Basics
Java
Java was developed in the early 90s by Sun Microsystems Java is a high-level language Java programs are portable across platforms
Each program is translated into Java bytecode Each machine has a Java Virtual Machine (JVM) which knows how to execute Java bytecode
Java is object-oriented
We will not use objects in this class
A Java program
/* Here you describe what your program does. Laura Toma Csci107 */
public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class
Then you execute it That is, you call the Java Virtual Machine to interpret and execute the Java bytecode of your program Example: prompt>java Hello
Notes
Comments
what follows after // on the same line is considered comment Or, what is in between /* this is a comment */
Indentation
is for the convenience of the reader; compiler ignores all spaces and new lines ; the delimiter for the compiler is the semicolon
All instructions end by semicolon Lower vs. upper case matters!! Void is different than void Main is different that main
Example
/* This program illustrates the System.out.println command. */
public class Hello { public static void main (String args[]) { System.out.println(This is my first Java program!); System.out.print(I like Java.); System.out.print(I think Java is cool.); } // end of main } // end of class
Variable declaration
type variable-name; Meaning: variable <variable-name> will be a variable of type <type> Where type can be: int double
Example
/* Printing ages. */
public class MyFirstJavaProgram { public static void main (String args[]) { int myAge, myFriendAge; /* declare two integer variables */ myAge = 20; myFriendAge = myAge + 1; //one year older System.out.println(Hello, I am +myAge + years old, and my friend is myFriendAge + years old); System.out.println(Goodbye);
If statements
True
if (condition) { //instructions } else { //instructions } //instructions
condition
False
instructions
instructions
instructions
Boolean conditions
..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Example:
int x, y; //two variables //assume they have some values if (x <= y) { System.out.println(x is smaller); } else { System.out.println(x is larger); }
Class work
Write a program that asks for two scores and prints out the loser and the winner. For example, it could look like this (or use your imagination!) . Make sure you handle all cases (victory, loss, tie).
Tell me Bowdoin score: 23 Tell me Bates score: 16 Congratulations, Bowdoin won!! Goodbye.
Class work
Write a program to compute the miles-per-gallon gas consumption of a car during a trip. Your program should ask the user for start and end mileage, and the amount of gas consumed. Then it should print out the result, and an appropriate message, depending whether the consumption is lower, equal to, or greater than 25 miles-per-gallon.
Enter the start mileage of your trip: 56345 Enter the end mileage of your trip: 56987 Enter the number of gallons you filled the tank: 20.7 Your car gives 31.15 miles-per-gallon. Congratulations!! Goodbye.