****************Need JCreator for some of the codes**********************
Created by: Purez/Justin
An Integrated Devoelopment Environment
File Hello.java - Code:
-
public class Hello
{
public static void main(String[]args)
{
//display a greeting in the console window
System.out.println("Hello, World!");
}
}
A simple program - Code:
-
public class ClassName
{
public static void main(String[]args)
//Comment
Method call
object.methodName(parameters)
System class
System.out object
pringln method
Syntax 1.1: Method Callobject.methodName(parameters)
Example: -System.out.println("Hello, Dave!");
Number types-int: integers, no fractional part
1, -4, 0
-double: floating-point numbers (double precision)
0.5, -3.11111, 4.3E24, 1E-14
Assignment operatorint nickels = 0;
int dimes = 0;
int quarters = 0;
nickels = nickels+count
Increment/Decrement-nickels++ is the same as nickels = nickels + 1
-nickels-- decrements the content of the variable
Constantsfinal double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.1;
final double QUARTER_VALUE = 0.25;
double money = 0;
money = nickels * NICKEL_VALUE + dimes*DIME_VALUE + quarters + QUARTER_VALUE;
Division and Remainder-/ is the division operator
-if both arguments are integers, the result is an integer. The remainder is discarded
7.0 / 4 = 1.75
7 / 4 = 1
Get the remainder with % (pronounced "Modulo")
7 % 4 = 3
Mathematical FunctionsType Conversion-in assignment, types must match.
double total = "a lot"; //no
-Use "cast" (int) to convert floating-point values to integer values:
int pennies
= (int)(total * 100);
-Cast discards fractional part.
-Use Math.roun for rounding:
int dollar =
(int)Math.round(total);
Strings-Strings constants:
"Carl"
-String variables:
String name = "Carl";
-String length:
int n = name.length()
Concatenation-String fname = "Harry"'
String iname = "Hacker";
String name = fname +iname;
-Name is "HarryHacker"
-if one operand of + is a string, the other is converted to a string:
String a = "Agent";
String name = a + 7;
-name is Agent7
Converting between Strings and Numbers-Convert to number:
int n = integer.parseint(str);
double x = Double.parseDouble(x);
-Convert to string
String str = "" + n;
str = Integer.toSTring(n);
Substrings-String greeting = "Clown";
String sub = greeting.substring(1, 4);
-Supply start and "past the end" position
-First position is at 0
1-C, 2-L, 3-O, 4-W, n
-substring length = "past the end" - start
Reading Input-String input = JOptionPane.showIntputDialog(prompt)
-Convert strings to numbers if necessary:
int count = Integers.parseInt(input);
-Conversion throws an exception if user doesn't supply a number
-Add
System.exit(0)
to the main method of any program the uses JOptionPane
An input DialogFile InputTest.java - Code:
-
Import javax.swing.JOptionPane
/**
this program tests input from an input dialog.
*/
public class InputTest
{
public static void main(String[]args)
{
Purse myPurse = new Purse();
String input = JOptionPane.showInputDialog(“How many nickels do you
have?”);
Int count = Integer.parseInt(input);
my Purse.addNickels(count);
input = JOptionPane.showInputDialog(“How many dimes do you have?”);
count = integer.parseInt(input);
myPurse. addDimes(count);
input = JOptionPane.showInputDialog(“How many quarters do you have?”);
count = integer.parseInt(input);
myPurse.addQuarters(count);
double totalValue = myPurse.getTotal();
System.out.println(“The total is “ + totalValue);
System.exit(0);
}
}
Characters-char: character type-a single Unicode character
-Character constants use single quotes:
'A', '\n','\u00E9'
-'A' is not the same as "A"
-charAt method gets character from a string "Hello".charAt(0) is 'H'
Copying numbers-double balance1 = 1000;
double balance2 = balance1;
balance2 = balance2 + 500;
-Change in balance2 does not affect balance1
Message Box Output-JOptionPane.showMessageDialog(null. per.toString(), "Testing inheritence", JOptionPane.INFORMATION_MESSAGE);
-Message types
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE
Hope this helped it took me about a hour to think it up
(Im suspended from school so I havnt been on ds or any computers till today while moms gone
)