Java Programming: / This Is My First Program I Am Excited

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Java Programming

/** this is my first program I am excited */ public class JavaRules { public static void main(String[] args) { System.out.println ("java is the best OOP language"); } }

Introduction to Java

// this is my first java program comment 1 /* I am excited what about you comment 2*/ /** this is used as a doc comment comment 3 */

Introduction to Java
public static void main(String[] args) public one of the methods must be named main; the program starts at main; main can be called from outside the class static main is a class method void main does not return a value String [] main will be provided with an array of characters when the program begins to execute

Introduction to Java
System.out.println ("java is the best OOP language"); A command/statement System.out javas name for the standard place to display messsages println method that displays output; print and advance to the next line

Introduction to Java
Program Layout Comments Tokens public class JavaRules { (4 tokens); comments are not tokens Indentation and brace placements JavaRules consist of a statement nested inside a method, main, inside a class, javaRules

Introduction to Java
Using variables Type, name, semicolon eg. Int i; int i=0, j, k=0; Java expects that variables are initialize prior to their first use Do not declare a variable twice System.out.print vs system.out.println

Introduction to Java
Types int a variable type which stores a single variable; -2,147,483,648 and 2,147,438,647 double: 1.79 x 10 **308 float boolean: true or false char: stores a single character char ch = w Literals 0 a 48. .48e2 often used as initializers

Introduction to Java
Identifiers names chosen by the programmer May contain letters, numbers and underscore No limit to length Lowercase is not uppercase Begins with a letter or underscore last_index, LastIndex keywords

Introduction to Java
Assignment Operators double fah = 98.6 double cel = (fah 32.0) * (5.0 / 9.0) final double FrePt = 32.0

Introduction to Java
System.out.println(); \n is the new line character System.out.println(hello\nthere\nyou); System.out.println(hello\nthere\n\nyou); System.out.println(hello\special\there\nyou ); System.out.println(hello\nthere\\you);

Introduction to Java
import jpb.*; SimpleIO.prompt (Enter Number);

import java.io.*; import java.util.Scanner; import java.io.IOException; public class ftoc { public static void main(String[] args) { int fahrenheit; Scanner input = new Scanner (System.in); final int Freezing = 32; final int Ratio = 5; System.out.println("Enter Fahrenheit"); fahrenheit = input.nextInt(); int celsius = (fahrenheit - Freezing) * Ratio; System.out.println("Celsius equivalent: " + celsius); } }

public class PrimeNumber { public static void main(String[] args) { int num = 11; int i; for (i = 2; i < num; i++) { int n = num%i; //System.out.println(n); if (n==0) { System.out.println("not prime!"); break; } } if (1==num){ System.out.println("prime!"); } } }

public class PrimeNumber { public static void main(String[] args) { int num = 24; int i; for (i = 2; i < num; i++) { int n = num%i; System.out.println(n); if (n==0) { System.out.println("not prime!"); } else { System.out.println("prime!");

} }
}

// Generates a random number and prints whether it is even or odd if ((int)(Math.random()*100)%2 == 0) { System.out.println(Number is even"); } else { System.out.println(Number is odd"); }

import java.util.*; import java.io.*; public class oranges{ public static void main(String[] args) { int num; int x=1; do { try { Scanner input = new Scanner (System.in); System.out.println("enter first number"); int num1 = input.nextInt(); System.out.println("enter second number"); int num2 = input.nextInt(); int answer = num1/num2; System.out.println("answer = " + answer); x=2; } catch (Exception e){ System.out.println("you did something wrong try again"); } } while (x == 1); }

You might also like