simple java programming

 A. Java Programming Writing Rules

In Java there are rules for writing variable names. Variable names in Java programs must be written connected without spaces. In addition, variable name writing also complies with the rules for writing variable names, namely with the rule that the first letter is small and the initial letter of the second word in the variable is written in capital letters. Similarly, the initial letter in the 3rd word and so on begins with a capital letter. The same thing also applies in writing the name of the scanner.


Writing example:

Variable name = areaOfSquare, integerNumber, decimalNumber, etc.

Scanner name = keyboardData, keyboard, user input, etc.

   However, it is different in writing for java class names and folder names where the rules for writing names begin with uppercase letters, as well as the initial letter in the second word and so on. The writing of java class names and folder names must also be connected without spaces.

Example of writing:

Folder name = TutorialForJava, JavaForBeginner, etc.

Class name = LearningAboutJava, JavaProgramNumber1, etc.


In addition to the above, in java to display the output, we can use 2 basic types of System.out() which are System.out.print() and System.out.println(). In displaying the output results, we can add information or writing in the output, using the “statement contents”. If you want to add a variable that you want to display the output, you can do so by adding “+” at the beginning of the variable name that you want to display.

Example of writing :

System.out.print("Integer is ”+VariableName)

System.out.print("Integer is ”+variableName)



1. Example Of A Java Program With Integer Type


A simple Java program of type integer(Integer)


 


This program uses the integer data type to input or generate integers. Therefore, we use the integer data type to initialize an integer.


Steps:

Declare the Java class public class NameJavaProgram {}.

Declare the main program void public static void main(String[] args) {}.

Declaring a variable of type int(integer) int nameVariable and initializing an integer.

Create an output printout with System.out.print() to create a one-line printout and System.out.println() to create a new-line printout for the next printout. Inside add “+” variable name to display the initialized integer output which is “2”.

Run the program by right-clicking on the mouse and clicking run or ctrl+shift+f6.


The output of the program:


 



1a. Java Program By Requesting Input From The User



This java program uses integer data types as well, but this time the input or initialization of integers comes from user input. Therefore, we use a scanner to read the input from the user.


Steps:

Import the scanner item or package by declaring import java.util.Scanner to read input from the user.

Declare the Java class public class NameJavaProgram {}.

Declare the main program void public static void main(String[] args) {}.

Declared a variable of type int(integer) int nameVariable to store integer input.

Declare the Scanner name with Scanner nameScanner = new Scanner(System.in)

Declare the variable to be filled in by the user with nameVariable = nameScanner.nextInt().

Display the output to prompt the user to enter an integer.

Print the output with System.out.print() to create a one-line printout and System.out.println() to create a new-line printout for the next printout. Inside add “+” variable name to display the inputted integer output.

Run the program by right-clicking on the mouse and clicking run or ctrl+shift+f6.


The output of the program:


 



2. Example Of A Java Program With Double Type


Simple Java program of type double (not integer)


 


This program uses the double data type to input or generate an integer number. Therefore, we use the integer data type to initialize an integer.


Steps:

Declare the Java class public class NameJavaProgram {}.

Declare the main program void public static void main(String[] args) {}.

Declaring a variable of type double with double nameVariable and initializing non-integer numbers.

Create an output printout with System.out.print() to create a one-line printout and System.out.println() to create a new-line printout for the next printout. Inside add “+” variable name to display the output of the initialized integer number “2.7”.

Run the program by right-clicking on the mouse and clicking run or ctrl+shift+f6.


The output of the program:




2a. Java Program By Requesting Input From The User


 


This java program uses the double data type as well, but this time the input or initialization of the non-integer number comes from user input. Therefore, we use a scanner to read the input from the user.


Steps:

Import item or package by declaring import java.util.Scanner to read user input.

Declare Java class public class NameJavaProgram {}.

Declare the main program void public static void main(String[] args) {}.

Declaring a variable of type double with double nameVariable to store the input of non-integer numbers.

Declare the Scanner name with Scanner nameScanner = new Scanner(System.in)

Declare the variable to be filled in by the user with nameVariable = nameScanner.nextDouble()

Display the output to prompt the user to enter a non-integer (decimal) number.

Print the output with System.out.print() to create a one-line printout and System.out.println() to create a new-line printout for the next printout. Inside add “+” variable name to display the inputted non-integer output.

Run the program by right-clicking on the mouse and clicking run or ctrl+shift+f6.


Program output of the program:




3.  Java Program Example Calculating Square Area





This java program uses a variable of integer data type to store input from the user and a variable of integer type to store the result of the square area calculation operation. In this program, the program will display the output to ask the user to input the side of a square.  After the input is entered, the program will execute the input in generating the area of a square with the formula s2 which is areaOfSquare = side * side. Thus the program will produce the following output:




4. Java Program Example Calculating The Area Of A Rectangle





This java program uses integer data type variables to store input from the user and integer type variables to store the results of the rectangle area calculation operation. In this program, the program will display the output to ask the user to input the sides of a rectangle, namely side A and side B.  After the input is entered, the program will execute the input in generating the area of the rectangle with the formula side A x side B, namely areaOfTriangle = sideA * sideB. Thus the program will produce the following output:





5. Java Program Example Calculating Triangle Area




This java program uses a double data type variable to store input from the user and a double data type variable to store the result of the triangle area calculation operation. In this program, the program will display the output to ask the user to enter the base and the high side of a triangle.  After the input is entered, the program will execute the input in generating the triangle area with the formula ½ x base x height, namely areaOfTriangle = 1 * highSide * baseSide / 2. Thus the program will produce the following output:



 7. Java Program Operation Using If-ELSE (BRANCHING)

In java, there is one method of operating program conditions, namely If-Else. If-Else is a program processing system where the program will be executed in branching a condition from user input with predetermined criteria. In the process, the program will read the input from the user where the program will execute the input in a branching with the criteria specified in the program.


7b. IF-ELSE PROGRAM EXAMPLE


                                                 PROGRAM DETERMINES AGE






 


From the program, the program asks the user to enter the names of the two people and the ages of the two people. The name is stored in a String type variable that functions to store a number of letter characters, and the age is stored in an integer variable. In the process, after the user inputs both the name and age of the two people, the program will look for a condition that matches the criteria specified in the branching, which is implemented in the following branching program:

 




Condition 1: If the age of the first person is greater than the second person, then print the name of the first person older than the second person.

Condition 2 : But if the age of the first person and the second person are the same age, then print the first person and the second person are the same age.

Condition 3: But otherwise if the input branching result does not match the conditions 1 and 2 that have been determined criteria, it means that the age of the first person is not older than the second person and the first and second person are not the same age, then print the name of the first person younger than the second person.


Output :

 



this is an example of programming in Java and you can modify it from us. hopefully with this tutorial, you can understand the Java programming language and can make ana become motivated as a programmer.









No comments:

Post a Comment

basic java programming tutorial for beginners

A. What is Java? Java is a programming language created by James Gosling. Java's syntax is similar to C/C++ because Java is heavily insp...