Posts

Showing posts with the label JAVA

Identifiers and Variables

Image
Identifiers are the names that identify the elements such as classes, methods, and variables in a program. All identifiers must obey the following rules: An identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. (See Appendix A for a list of reserved words.) An identifier cannot be true, false, or null. An identifier can be of any length. For example, $2, ComputeArea, area, radius, and print are legal identifiers, whereas 2A and d+4 are not because they do not follow the rules. Since Java is case sensitive, area, Area, and AREA are all different identifiers. Variables are used to store values that may be changed in the program. The variable declaration tells the compiler to allocate appropriate memory space for the variable based on its data type. A v

JAVA : Reading Input from the Console

Image
Reading Input from the Console Reading input from the console enables the program to accept input from the user. Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows: Scanner input = new Scanner(System.in); Above statement creates a Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method on an object (variable input) is to ask the object to perform a task. You can invoke the nextDouble() method to read a double value as follows: double radius = input.nextDouble(); Program : ComputeAreaWithConsoleInput.java  The specific import specifies a single class in the import statement. The wildcard import imports all the classes in a package by using the asterisk as the wildcard. There is no performance difference between a specific import and a wildcard import declaration. Program : ComputeAverage.java

Programming Errors

Image
1. Syntax Errors Errors that are detected by the compiler are called syntax errors or compile errors. Program : ShowSyntaxErrors.java Error1: Invalid method declarations: return type required (void is missing at line3) Error2: Unclosed string literal (string is not enclosed with double quotes in print statement at line 4) Common Error 1: Missing Braces Common Error 2: Missing Semicolons Common Error 3: Missing Quotation Marks Common Error 4: Misspelling Names 2. Runtime Errors Runtime errors are errors that cause a program to terminate abnormally. For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors (input errors) to occur in the program. Another example of runtime errors is division by zero. Program : ShowRuntimeErrors.java 3.Logic Errors Logic errors occur when a program does not perform the way it was intended to. You will get Fahrenheit 67 degr

UNIT-1 Introduction to java and elementary programming

Image
UNIT - 1 Introduction to java and elementary programming