















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















!
2
add, subtract, multiply, divide
6.022e floating-point numbers double add, subtract, multiply, divide 17 12345 int integers and, or, not true false boolean truth values sequences of characters characters type set of values literal values operations compare 'A' '@' char String (^) concatenate "Hello World" "126 is fun"
4
7
% java Ruler 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 "1 2 1" "1 2 1 3 1 2 1" "1" public class Ruler { public static void main(String[] args) { String ruler1 = "1"; String ruler2 = ruler1 + " 2 " + ruler1; String ruler3 = ruler2 + " 3 " + ruler2; String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler4); } } (^) string concatenation
10 public class IntOps { public static void main(String[] args) { int a = Integer.parseInt(args[ 0 ]); int b = Integer.parseInt(args[ 1 ]); int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(a + " + " + b + " = " + sum); System.out.println(a + " * " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " % " + b + " = " + rem); } }
command-line arguments Java automatically converts a, b , and rem to type String % javac IntOps.java % java IntOps 1234 99 1234 + 99 = 1333 1234 * 99 = 122166 1234 / 99 = 12 1234 % 99 = 46
13
http://java.sun.com/javase/6/docs/api/java/lang/Math.html
14
public class Quadratic { public static void main(String[] args) { // parse coefficients from command-line double b = Double.parseDouble(args[ 0 ]); double c = Double.parseDouble(args[ 1 ]); // calculate roots double discriminant = bb - 4.0c; double d = Math.sqrt(discriminant); double root1 = (-b + d) / 2.0; double root2 = (-b - d) / 2.0; // print them out System.out.println(root1); System.out.println(root2); } }
roots = " b ± b 2 " 4 c 2
17
19
public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[ 0 ]); boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0 ) && (year % 100 != 0 ); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0 ); System.out.println(isLeapYear); } } % java LeapYear 2004 true % java LeapYear 1900 false % java LeapYear 2000 true