Awe! it’s Java

What’s up  readers ?!

In this post,  I’m gonna tell you my first experience learning a high level programming language. Yep, it’s Java.

Do you know what Java is ?

1_iIXOmGDzrtTJmdwbn7cGMw

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

Mrs. Citra explained to us about the structure of Java patiently, she taught  us to write the code by ourselves. And ta – da !

we could make our first program Java

class FirstJavaProgram{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

You may be wondering why we have named the file as FirstJavaProgram, the thing is that we should always name the file same as the public classname. In our program, the public class name is FirstJavaProgram, that’s why our file name should be FirstJavaProgram.java.

We also learned about the if conditional in java. Here’s the syntax :

if(Boolean_expression) {
   // Executes when the Boolean expression is true
}else {
   // Executes when the Boolean expression is false
}

Although Java is object oriented, not all types are objects. It is built on top of basic variable types called primitives.

Here is a list of all primitives in Java:

  • byte (number, 1 byte)
  • short (number, 2 bytes)
  • int (number, 4 bytes)
  • long (number, 8 bytes)
  • float (float number, 4 bytes)
  • double (float number, 8 bytes)
  • char (a character, 2 bytes)
  • boolean (true or false, 1 byte)

Java is a strong typed language, which means variables need to be defined before we use them.

String is not a primitive. It’s a real type, but Java has special treatment for String.

Every comparison operator in java will return the type boolean that not like other languages can only accept two special values: true or false.


There are also types of operators in Java :

1) Basic Arithmetic Operators
2) Assignment Operators
3) Auto-increment and Auto-decrement Operators
4) Logical Operators
5) Comparison (relational) operators
6) Bitwise Operators
7) Ternary Operator

As a beginner, we need to take a look at the 1) and 4) to make us understand easier. We’ll talk about more operators in the next post.

  • Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %

+ is for addition.

– is for subtraction.

* is for multiplication.

/ is for division.

% is for modulo.

Note: Modulo operator returns remainder, for example 10 % 5 would return 0

  • Logical Operators

They are mainly used in conditional statements and loops for evaluating a condition.

Logical operators in java are: &&, ||, !

&& means and
|| means or

Let’s say we have two boolean variables b1 and b2.

b1&&b2 will return true if both b1 and b2 are true else it would return false.

b1||b2 will return false if both b1 and b2 are false else it would return true.

!b1 would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.


Not only reading, we also got some exercise to make us learning by doing. It was kinda fun but exhausting hehe. 

Take a loook at some questions below :

Make a  program Week5_3.java  which greet users based on their name, gender and their relationship status.

import java.util.Scanner;

public class Week5_3 {

    public static void main(String[] args) {
        System.out.print("What's your name? ");
        Scanner scan = new Scanner(System.in);
        String name = scan.next();
        System.out.print("Gender (M/F)? ");
        String gender = scan.next();
        System.out.print("Married? (Y/N)? ");
        String married = scan.next();
        String degree;
        if (gender.equalsIgnoreCase("M")) {
            gelar = "Mr.";
            System.out.println("Hello " + degree + name);
        } else if (gender.equalsIgnoreCase("F") && (married.equalsIgnoreCase("Y"))) 
           { gelar = "Mrs.";
            System.out.println("Hello " + degree + name);
        } else {
            gelar = "Ms.";
            System.out.println("Hello " + degree + name);
        }
    }
}

Make a program Week5_2.java to find smallest of 3 numbers:
Num1 : 6                             Num1 : 3                               Num1 : 9

Num2 : 1                             Num2 : 7                               Num2 : -3

Num3 : 10                           Num3 : 2                               Num3 : 5

Smallest number : 1       Smallest number : 2          Smallest number : -3

import java.util.Scanner;

public class Week5_2 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int a;
int b;
int c;
int min;

System.out.print("Num1: ");
a = scan.nextInt();
System.out.print("Num2: ");
b = scan.nextInt();
System.out.print("Num3: ");
c = scan.nextInt();

if (a<b && a<c) {
                 min = a;
                 System.out.println("Smallest number: " + min);
                }

else if (b<c){
               min = b;
               System.out.println("Smallest number: " + min);
             }
else {
               min = c;
               System.out.println("Smallest number: " + min);
     }
  }
}

 

Make program in Week5_6.java that can calculate area of either circle (1), rectangle (2), or triangle. (3)
a. If user chooses circle then ask its radius
b. If user chooses rectangle then ask its height and width
c. If user chooses triangle then asks its height and width
d. If user’s input is negative number, give an error message then the algorithm reaches end.

Example 1:
Choose a Shape:
1 – Circle
2 – Rectangle
3 – Triangle
Choose : 1
Radius : 10
Area = 314.15927

Example 2:
1 – Circle
2 – Rectangle
3 – Triangle
Choose : 3
Width : 10
Height : 5
Area = 25

import java.util.Scanner;
public class Week5_6 {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int height = 0;
int width;
double radius;
double area1;
int area2;
int area3;
int answer;

System.out.println("Shape: ");
System.out.println("1 - Circle");
System.out.println("2 - Rectangle");
System.out.println("3 - Triangle");
System.out.println("Answer : ");
answer = scan.nextInt();

if (answer == 1) {
System.out.println("radius?");
radius = scan.nextInt();
if (radius >= 0) {
area1 = radius * 3.14;
System.out.println("area: " + area1);
} else {
System.out.println("INPUT ERROR");
}
} 
else if (answer == 2) 
{
System.out.println("width?");
width = scan.nextInt();
System.out.println("height?");
height = scan.nextInt();
if (width <= 0 && height <= 0) 
{
System.out.println("INPUT ERROR");
} 
else 
{
area2 = width * height;
System.out.println("area: " + area2);
}
}
else if (answer == 3) {
System.out.println("width?");
width = scan.nextInt();
System.out.println("height?");
height = scan.nextInt();
if (width <= 0 && height <= 0) 
{
System.out.println("INPUT ERROR");
} 
else 
{
area3 = width * height/2;
System.out.println("area: " + area3);
}
}
}
}

Make a program Week5_10.java to calculate the distance between 2 airplanes which x and y each airplane is being input.

Example :

Plane 1
X : 2
Y : 8
Plane 2
X : 6
Y : 2

Distance = 7.211102550927978

import java.util.Scanner;

public class Week5_10 {
public static void main(String[] args) {
int x1;
int y1;

int x2;
int y2;
double distance;

Scanner scan = new Scanner(System.in);
System.out.println("X plane 1 ?");
x1= scan.nextInt();
System.out.println("Y plane 1 ?");
y1= scan.nextInt();

System.out.println("X plane 2 ?");
x2= scan.nextInt();
System.out.println("Y plane 2 ?");
y2= scan.nextInt();

double x = x2-x1;
double y = y2-y1;
distance = Math.sqrt((x*x) + (y*y));
System.out.println("The distance within 2 airplanes" + distance);
}
}

We need to be patient to code a program, because if we miss a single semicolon; the programs wouldn’t run. Here are some  important stuffs  that I got in learning Java :

  • We should always name the file same as the public class name
  • We only need one psvm in every class to put our codes below
  • Each variable can only contain one data type
  • Variables in Java are case sensitive so upper case letter would be different
  • We need to define the variables before using them

Thanks for visiting my site, don’t forget to give a thumb up and leave a comment below.

Have a great day !



” Everybody in this country should learn to program a computer,

because it teaches you how to think “

Steve Jobs




Self – reflection :

I need to practice and learn about Java more so I can get used to put semicolon at the end of every command line.

Special thanks to Mrs. Citra and Mr. Mychael




src :

https://www.java.com/en/download/faq/whatis_java.xml

https://beginnersbook.com/2013/05/first-java-program/

https://www.tutorialspoint.com/java/if_else_statement_in_java.htm

https://www.learnjavaonline.org/en/Variables_and_Types

https://beginnersbook.com/2017/08/operators-in-java/

https://beginnersbook.com/2017/08/if-else-statement-in-java/

https://www.thecrazyprogrammer.com/2015/02/programming-quotes.html

2 thoughts on “Awe! it’s Java

  1. Wow.. your blog is very good. it’s easy to read and easy to understand. You inspired me to make a better blog.
    Maybe you can teach me how to write some articles with English because my English is suck :v
    Good Job, Angel.
    God Bless You.

    Like

    1. tysm, David.
      My english ain’t that good either.
      If you want to write some articles in english, you just need to start it now cause we can learn more grammar by writing.
      Mistake makes us better.
      Gbu too!

      Liked by 1 person

Leave a comment