Class




The Class in Java is an Object Oriented Wrapper. A Java program is made up of one or more classes. In general, a class contains instance variables (to hold data) and methods (to carry out an action). A class is kind of like a blue print or template. Every time an object is built at runtime, the blue print or template is used so that we know how to build the object. Just like a builder would use the blue prints when building a house or some other kind of building. The house or the building is the actual object. The code that a programmer writes is known as the source code. A compiler program translates this source code into either machine code or something close to machine code (like byte code in the case of Java) which is what the CPU (central processing unit) can process. Java Byte code is really close to machine code, but requires an interpreter at runtime to break it down into the actual machine code that the cpu understands. The Java interpreter (or run time system) is called the JVM (Java Virtual Machine). The JVM also has other tasks that it performs (besides just translating our byte code to machine code). It also handles passing events to our appropriate methods, handling garbage collection, etc.

Examples:


// A short console based Java program is shown below.

import java.util.Scanner;

public class Sample2 {
        
    public Sample2() {
    }
    
    public static void main(String[] args) {
        
        Scanner scan = new Scanner (System.in);
        
        System.out.println("Semester Grade");
        System.out.println();
        System.out.println();       

        System.out.print("Enter your semester 1 grade ");
        double s1 = scan.nextDouble();
        System.out.println();
        System.out.print("Enter your semester 2 grade ");
        double s2 = scan.nextDouble();
        System.out.println();
        System.out.print("Enter your final exam grade ");
        double exam = scan.nextDouble();
        System.out.println();
        System.out.println();
        double grade = (s1 + s2) / 2 * .8 + exam * .2;
        System.out.println("Your semester grade is " + grade );
        System.out.println();
        System.out.println();
        
    }
}




// Another short console based Java program is shown below. import java.util.Scanner; public class Sample3 { public Sample3() { } public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("Slope of a Line"); System.out.println(); System.out.println(); System.out.print("Enter x1 "); double x1 = scan.nextDouble(); System.out.print("Enter y1 "); double y1 = scan.nextDouble(); System.out.println(); System.out.print("Enter x2 "); double x2 = scan.nextDouble(); System.out.print("Enter y2 "); double y2 = scan.nextDouble(); System.out.println(); System.out.println(); if (x2-x1==0) { System.out.println("The slope of the line is undefined"); } else { double slope = (y2-y1)/(x2-x1); System.out.println("The slope of the line is " + slope); } System.out.println(); System.out.println(); } }
// Another short console based Java program is shown below. // this is a comment // comments are ignored by the compiler (translator) import java.util.Scanner; public class Sample8 { public Sample8() { } public static void main(String[] args) { // we will create a Scanner object to read from the keyboard Scanner scan = new Scanner (System.in); // this is the title System.out.println("Rectangles"); System.out.println(); System.out.println(); // we will now ask the user to enter the length System.out.print("Enter the length "); double l = scan.nextDouble(); System.out.println(); System.out.print("Enter the width "); double w = scan.nextDouble(); System.out.println(); System.out.println(); double p = (2*l)+(2*w); System.out.print("The perimeter is " + p ); System.out.println(); double a = l*w; System.out.print("The area is " + a + " square units" ); System.out.println(); System.out.println(); System.out.println(); } }
// The following MyPoint class holds the coordinates // for a point on a 2D plane. public class MyPoint { // instance variables // storage is always available // for the life of an object of this type int x; int y; // constructor MyPoint with 2 parameters // this.x refers to the instance variable x // this.y refers to the instance variable x // x and y refer to the temporary parameter variables x and y // which only exist when the constructor is called (invoked) public MyPoint(int x, int y) { this.x = x; this.y = y; } // method getX() will return an int // which is the value of the instance variable x // we do not need to say this.x because there is no local variable x // this is called a getter method since it returns the value of an instance variable public int getX() { return x; // or we could say return this.x; } // method setX(int x) does not return a value // it receives a value and then assigns that value to the instance variable x // this is called a setter method since it sets the value of an instance variable public void setX(int x) { this.x = x; } // other methods not shown }