A Sample Class




The Class in Java is an Object Oriented Wrapper that can hold variables and methods. You can think of it as a container (a box). A computer program in Java is made up of 1 or more classes.

Example 1:



import java.util.Scanner;

public class Sample {
        
    // a constructor or initializer
    // a constructor has no return type, not even void
    // a constructor has the same name as the class
    // a constructor is called immediately after new
    // it's purpose is to initialize instance variables
    public Sample() 
    {
	    // code can go here
    }
    
    // the main method is the starting point
    // this is executed when the program is first run
    public static void main(String[] args) 
    {
        // create some local variables
        double m; // holds the slope of the line
        double x; // holds the x value
        double b; // holds the y intercept
        double y; // holds the y value
		
        // create a Scanner object in memory
        // that can read data from the keyboard		 
        Scanner scan = new Scanner (System.in);
		
        System.out.println("The Slope of a Line");
        System.out.println();
        System.out.println();
		
        System.out.print("Enter the slope ");
        m = scan.nextDouble();
        
        System.out.print("Enter the value of x ");
        x = scan.nextDouble();
        
        System.out.print("Enter the value of b ");
        b = scan.nextDouble();
        
        y = m*x + b;
        System.out.println("The value of m is " + m);
        System.out.println("The value of x is " + x);
        System.out.println("The value of m is " + m);
        System.out.println("The value of y is " + y);
    }
}





Example 2:

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(); } }

Example 3:

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(); double slope = (y2-y1)/(x2-x1); // x2!=x1 System.out.println("The slope of the line is "+slope); } } Example 4 // The following MyPoint class holds the coordinates // for a point on a 2D plane. public class MyPoint { // instance variables int x; int y; // constructor MyPoint with 2 parameters // this.x refers to the instance variable x // x refers to the temporary parameter variable x public MyPoint(int x, int y) { this.x = x; this.y = y; } // other methods not shown }