/* * The above slash-star starts a multi-line comment. * * Basics in Java: special symbols, data types, * operators, expressions, input/output, and Strings. * * CSCI 51, Stough * January 27, 2009 * * The below star-slash ends the multi-line comment. */ import java.util.Scanner; //The Scanner data type is used to get input //from the user. public class BasicsJava { // *data types*: the basic kinds of data (number, character, etc.) that // a *variable* stores public static void main(String[] args) { // *variable*: a named location storing some value. see naming rules. int i = 1; // i is an integer/whole number, and is assigned the val 1. double d; // d is a double-precision floating point number - e.g. 12.2 char c; // c is a typing character, such as '+' or the letter 'a'. // *special symbols*: +, -, /, *, &, !, cannot be used in variable name // *expression*: collection of variables, literals, and operators, that // can be evaluated to some data type. i = 12 * 7 + 5; //i is set to be the result of evaluating the expr, //namely, i is set to 89. System.out.println("i is " + i); //output. the *parameter* sent is an //*expression* evaluating to a String d = (1.2347*i) - 10.7; //an expr involving floating point and integer //operands will be evaluated to a floating point. System.out.println("d is " + d); //output. System.out.printf("\tformatted to two decimal places, d is %.2f\n",d); //The above formats the output. See printf in the API. //'\t' forces an indentation, '\n' forces a carriage return, %.2f //says that I want a float with two places after the decimal. %f //by itself would say to output as is (with more decimal places). String s = "my new String has not too many words in it."; //s is a String containing the literal sequence of characters above. System.out.print(s + " And here are a few more.\n"); //print (without the ln) prints without the carriage return, so I //added it. Scanner scan = new Scanner(System.in); //just like int i, Scanner scan declares a new Scanner object/variable, //then the new Scanner actually constructs this object. int, double, //and other *non-capped data types* do not require the new operator //because they are *primitive*. System.out.print("Please give me the height and width of " + "a rectangle: "); double h = scan.nextDouble(); double w = scan.nextDouble(); //h and w contain the numbers entered //by the user. //Compute the area. double area = h * w; //pretty easy, area is a double set to be h*w. System.out.print("The area of a rectangle with height " + h + " and width " + w + " is " + area + ". Bye\n"); } //This brace ends the main method. } //This brace ends the BasicsJava class definition.