Java Programming I on YouTube:
https://www.youtube.com/playlist?list=PLLIqcoGpl73iaXAtS_-V_Xdx3mhTzPwb5
import java.util.Scanner;
/*
* Author: J. Murach
* Purpose: This program uses the console to get a subtotal
* from the user, and it calculates the discount amount and
* total and displays them.
*/
public class InvoiceApp
{
public static void main(String[] args)
{
// display a welcome message
System.out.println(
"Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line
// get the input from the user
Scanner sc = new Scanner(System.in);
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
// calculate the discount amount and total
double discountPercent = .2;
double discountAmount = subtotal * discountPercent;
double invoiceTotal = subtotal - discountAmount;
// format and display the result
String message = "Discount percent: " +
discountPercent + "\n" +
"Discount amount: " + discountAmount + "\n" +
"Invoice total: " + invoiceTotal + "\n";
System.out.println(message);
}//main
}//class InvoiceApp
Java program: collection of classes
There is a main method in every Java application program
All statements end with a semicolon.
Use proper white space indentation.
All blocks start with
{ // the beginning block mark } // the ending block mark
Statements within the braces are referred to as block of code, or statement block.
The rules for naming an identifier:
Start each identifier with a letter, underscore, or dollar sign. Use letters, dollar signs, underscores, or digits for subsequent characters.
Use up to 255 characters.
Don't use Java keywords.
InvoiceApp $orderTotal i Invoice _orderTotal x InvoiceApp2 input_string TITLE subtotal _get_total MONTHS_PER_YEAR discountPercent $_64_Valid
boolean if interface class true char else package volatile false byte final switch while throws float private case return native void protected break throw implements short public default try import double static for catch synchronized int new continue finally const long this do transient goto abstract super extends instanceof null
order Total // spaces are not allowed
counter@ // punctuation characters not allowed
2nd // identifier cannot begin with a digit
boolean // boolean is a keyword
Syntax:
public static void main(String[] args) { //statements... }
A class that is executable must provide a main method.
There can be only one main method in that class.
The class InvoiceApp provides a main method:
public class InvoiceApp // declare the class { // begin the class public static void main(String[] args) { System.out.println( "Welcome to the Invoice Total Calculator"); } } // end the class
Start the name with a capital letter.
Use letters and digits only.
Follow the other rules for naming an identifier.
Start every word within a class name with an initial cap.
Each class name should be a noun or a noun that is preceded by one or more adjectives.
You can name your class whatever you like, but remember you must save it in a file with the same name.
The class name InvoiceApp is stored in the file named InvoiceApp.java.
The method named "main" becomes the entry point into the application when JVM begins its execution.
The indentation and alignment within the class and the method is an important aspect of coding.
To declare and initialize a variable in one statement, use the following syntax:
type variableName = value;
For example,
int scoreCounter = 1; // initialize an integer variable double unitPrice = 14.95; // initialize a double variable
Recall that int and double are primitive data types.
Data types present a set of values together with a set of operations.
Integral data type deals with integers -- numbers without a decimal part. (Characters are also integral types.)
Floating-point data type deals with decimal numbers, such as 3.14
Boolean data type deals with logical values involved in decision making during program execution.
char // 2 bytes in memory
byte // 1 byte
short // 2 bytes
int // 4 bytes
long // 8 bytes
Note: all integral data types are primitive data types.
Floating-point data types are also primitive data types:
float // precision = 6 or 7 double // precision = 15
boolean // allows two values:
true
false
For example,
boolean isCalculationDone = false;
Assignment Operators change values of the variables. For example,
int quantity = 0; // initialize an integer variable int maxQuantity = 100; // initialize another integer // Statements with assignment: quantity = 10; // quantity is now 10 quantity = maxQuantity; // quantity is now 100
Start variable names with a lowercase letter and capitalize the first letter in all words after the first word.
Each variable name should be a noun or a noun preceded by one or more adjectives.
Try to use meaningful names that are easy to remember.
// integer arithmetic int x = 14; int y = 8; int result1 = x + y; // result1 = 22 int result2 = x - y; // result2 = 6 int result3 = x * y; // result3 = 112 int result4 = x / y; // result4 = 1
Note how dividing an integer by an integer produces an integer quotient and the remainder is lost.
// double arithmetic double a = 8.5; double b = 3.4; double result5 = a + b; // result5 = 11.9 double result6 = a - b; // result6 = 5.1 double result7 = a * b; // result7 = 28.9 double result8 = a / b; // result8 = 2.5
int result9 = invoiceTotal / invoiceCount; // result9 = 125 double result10 = invoiceTotal / invoiceCount; // result10 = 125.50
+ // addition - // subtraction * // multiplication / // division % // modulus
Unary operator: operator that has one operand
Binary operator: operator that has two operands
Operators can have
the same precedence
higher precedence
lower precedence
when used in combination. For example, an expression
2 + 3 * 4 // yields the value of 14
This is because multiplication operator has higher precedence than addition.
A programmer can override the built-in order of precedence by grouping subexpression with parenthesis:
( 2 + 3 ) * 4 // yields 20.
When operators have the same level of precedence, operations are performed from left to right
Expressiond are integral when all operands are integers. For example,
2 + 3 * 5 3 + x – y / 7 x + 2 * ( y – z ) + 18
All operands are floating-point numbers:
12.8 * 17.5 – 34.50 x * 10.5 + y - 16.2
Mixed Expressions have operands of different types. For example,
2 + 3.5 6 / 4 + 3.9
Integer operands yield an integer result; floating-point numbers yield floating-point results.
If both types of operands are present, the result is a floating-point number
Precedence rules are followed