Apd 545 Week 1

This is blog website for Application Development course (Seneca College)

ยท

5 min read

Installations and Setup

  1. JDK (Java developer kit) to run Java

  2. Eclipse ( IDE to write and run JAVA)

  3. Java Fx ( from gluonhq website) SDK version

  4. Scene Builder

Installations in Eclipse

type fx in eclipse marketplace which is under help and install the first one.

Connect Eclipse with Scene Builder

Add the path to Eclipse where your Scene Builder is located

  1. Click Windows -> preferences -> Javafx -> copy the path of Scene Builder and paste it

Introduction to Java

In Java, every application begins with a class name, and that class must match the filename.

// file - HelloWorld.java

// Java program always have at least one Public class
// Class name should always starts with capital letter
// compiler wants it to be capital

public class HelloWorld {

    public static void main(String[] args){
        System.out.println("Hello World");
        //System is a built-in Java class that contains useful members, such as out
        // println() method to print a line of text to the screen
    }

}
๐Ÿ’ก
Note: Java is case-sensitive: "MyClass" and "myclass" has a different meaning.

println() means print line and it will also add a new line.

Use the print() method if you do not want to insert a new line.

System.out.print("Hello World! ");
System.out.print("I will print on the same line.");

Java Variables

In Java, there are different types of variables, for example:

  • String - stores text, such as "Hello". String values are surrounded by double quotes

  • int - stores integers (whole numbers), without decimals, such as 123 or -123

  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99

  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

  • boolean - stores values with two states: true or false

Data Types:

Almost Same as C++

  1. Primitive Data type

  2. Non - primitive Data Type

Example:

byte myNum = 100;
//The byte data type can store whole numbers from -128 to 127.
short myNum = 5000;
//The short data type can store whole numbers from -32768 to 32767:
int myNum = 5;
//The int data type can store whole numbers from -2147483648 to 2147483647.
long myNum = 15000000000L;
//The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. 
// Note that you should end the value with an "L":
float myFloatNum = 5.99f;
//The precision of float is only six or seven decimal digits.
double myNum = 19.99d;
// double variables have a precision of about 15 digits
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";

Final Variables ( same as const)

If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will declare the variable as "final" or "constant", which means unchangeable and read-only):

final int myNum = 15;
myNum = 20;  // will generate an error: cannot assign a value to a final variable

Strings

A String in Java is actually an object, which contain methods that can perform certain operations on strings.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());

More String Methods

  1. toUpperCase() and toLowerCase()

  2. The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace).

     String txt = "Please locate where 'locate' occurs!";
     System.out.println(txt.indexOf("locate")); // Outputs 7
    
    ๐Ÿ’ก
    Java counts positions from zero same as C/C++
  3. The concat() method to concatenate two strings.

     String firstName = "John ";
     String lastName = "Doe";
     System.out.println(firstName.concat(lastName));
    

Example From Class

// file HelloWorld.java

package ca.senecacollege.first;

import java.util.Scanner;  // CTRL + SHIFT + O

import ca.senecacollege.models.Person;

public class HelloWorld {

    public static void main(String[] args) { // S capital so it is a class args is a object 
        // static means like we can call it without creating an instance of the class so it is like helloWorld.main()
        // can be multiple main in Java
        // C# they will look for MVC model it is a design pattern

        Person p1 = new Person(); // accessing the person class from models

        System.out.println("Hello World");// this println will print line with new line charac
        // final means constant we cant change it. so if a class has final than it cannot be changed
        System.out.println(p1);  // prints the address of the p1

        Scanner input = new Scanner (System.in);

        int x = input.nextInt();        
    }

}
package ca.senecacollege.models;

public class Person {

    private String name;
    // String is a class and there are different ways to initialize
    // name = new String();  //***** new key word not recommended takes extra space****// 
    // name = new String("Mike")
    // String name = "Mike";
    //Mike stays in the string pool everything else in the stack memory portion
    private String address;
    private int id;    
    // every primitive type has a wrapper class like int has Integer

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Override      
    public String toString() {
        return " The value of the Person are"+ name + "and" + id;

    }

}
ย