Java Examples 2

The Reason for Creating This: Unfortunately, Java Examples has a problem at the current moment where editing the document results in a 403 Forbidden Error. In order to add to the list of Java Examples, i'll continue putting up new code here, so enjoy!

Simple Cube Calculator

Calculates the cube of a program with predetermined values. Is an example of using constructors and objects

package com;

public class cubeClasses {
    public static void main(String[] args)
    {
        cube cubeObj1, cubeObj2; // creates two 'cube' variables
        cubeObj1 = new cube(); // initializes a new object w/ no parameters
        cubeObj2 = new cube(10, 20, 30); // initializes a new object w/ three parameters
        System.out.println("Volume of Cube 1 is: " + cubeObj1.getVolume()); // calls .getVolume method to calculate volume of cubeObj1
        System.out.println("Volume of Cube 2 is: " + cubeObj2.getVolume()); // calls .getVolume method to calculate volume of cubeObj2
    }
}
package com;

public class cube
{
    int intLength, intWidth, intHeight; // initializes three variables for calculating volume of cube

    public int getVolume() // method to return volume of a cube
    {
        return (intLength * intWidth * intHeight); // returns the volume of the cube
    }

    cube() // constructor w/ no parameters
    {
        intLength = 10; // sets the length of the cube to 10
        intWidth = 10; // sets the width of the cube to 10
        intHeight = 10; // sets the height of the cube to 10
    }

    cube(int l, int w, int h) // constructor w/ three parameters
    {
        intLength = l; // sets the length of the cube to a user defined parameter
        intWidth = w; // sets the width of the cube to a user defined parameter
        intHeight = h; // sets the height of the cube to a user defined parameter
    }
}

Imperial ⇐⇒ Calculator

This program uses predefined values in order to calculate several random unit of measurements back and forth:

package com;

public class MetricConversion
{
    public static void main(String[] args)
    {
        System.out.println("1 Feet =" + Change.doFeetMeters(1) + " Meters.");
        System.out.println("5280 Feet =" + Change.doFeetKm(5280) + " Kilometers.");
        System.out.println("39.6 Inch =" + Change.doInchMeter(39.6) + " Meters.");
        System.out.println("15 Mile =" + Change.doMilesKm(15) + " Kilometers.");
        System.out.println("10 Meter =" + Change.doMeterFeet(10) + " Feet.");
        System.out.println("-41 Celsius =" + Change.cToF(-41) + " Fahrenheit.");
        System.out.println("68 Fahrenheit =" + Change.fToC(68) + " Celsius.");
    }
}
package com;

public class Change
{
    private static double meterInFeet = 3.280839895;
    private static double feetInMile = 5280;
    private static double inchInFeet = 12;

    public static double doFeetMeters(double dblFeet)
    {
        return dblFeet / meterInFeet;
    }

    public static double doFeetKm(double dblFeet)
    {
        return (dblFeet / meterInFeet) / 1000;
    }

    public static double doInchMeter(double dblInches)
    {
        return doFeetMeters(dblInches / inchInFeet);
    }

    public static double doMilesKm(double dblMiles)
    {
        return doFeetKm(dblMiles * feetInMile);
    }

    public static double doMeterFeet(double dblMeters)
    {
        return dblMeters * meterInFeet;
    }

    public static double cToF(double temp)
    {
        return temp * 9/5 + 32;
    }

    public static double fToC(double temp)
    {
        return (temp - 32) * 5/9;
    }
}

Geometry Formula Calculator

Uses methods to plugin a value into a formula in order to calculate geometric equations.

package com;

import java.util.Scanner;

public class Geometry
{
    public static void main(String[] args)
    {
        double value1, value2;
        Scanner in = new Scanner(System.in);
        System.out.print("Input for Radius to find Circumference of Circle: ");
        value1 = in.nextDouble();
        System.out.println("Circumference: " + Formula.doCircumference(value1));

        System.out.print("Input for Radius to find Area of Circle: ");
        value1 = in.nextDouble();
        System.out.println("Area of Circle: " + Formula.doCircleArea(value1));

        System.out.print("Input for Area of Square: ");
        value1 = in.nextDouble();
        System.out.println("Area of Square: " + Formula.doSquareArea(value1));

        System.out.print("Input for Perimeter of Square: ");
        value1 = in.nextDouble();
        System.out.println("Perimeter of Square: " + Formula.doSquarePerimeter(value1));

        /*
        System.out.print("Input for Length of Rectangle to find Area: ");
        value1 = in.nextDouble();
        System.out.print("Input for Width of Rectangle to find Area: ");
        value2 = in.nextDouble();
        System.out.println("Area of Rectangle: " + Formula.doRectangleArea(value1, value2));

        System.out.print("Input for Length of Rectangle to find Perimeter: ");
        value1 = in.nextDouble();
        System.out.print("Input for Width of Rectangle to find Perimeter: ");
        value2 = in.nextDouble();
        System.out.println("Perimeter of Rectangle: " + Formula.doRectanglePerimeter(value1, value2));
        */

        // using overloaded doSquareArea methods
        System.out.print("Input for Length of Rectangle to find Area: ");
        value1 = in.nextDouble();
        System.out.print("Input for Width of Rectangle to find Area: ");
        value2 = in.nextDouble();
        System.out.println("Area of Rectangle: " + Formula.doSquareArea(value1, value2));

        // using overloaded doSquarePerimeter methods
        System.out.print("Input for Length of Rectangle to find Perimeter: ");
        value1 = in.nextDouble();
        System.out.print("Input for Width of Rectangle to find Perimeter: ");
        value2 = in.nextDouble();
        System.out.println("Perimeter of Rectangle: " + Formula.doSquarePerimeter(value1, value2));

        System.out.print("Input for Height of Triangle: ");
        value1 = in.nextDouble();
        System.out.print("Input for Base of Triangle: ");
        value2 = in.nextDouble();
        System.out.println("Area of Triangle: " + Formula.doTriangleArea(value1, value2));
    }
}
package com;

public class Formula
{
    private static double pi = 3.141592654;

    public static double doCircumference(double radiusCircumference)
    {
        return 2 * (pi * radiusCircumference);
    }

    public static double doCircleArea(double radiusArea)
    {
        return radiusArea * pi;
    }

    public static double doSquareArea(double lwArea)
    {
        return lwArea * lwArea;
    }

    public static double doSquarePerimeter(double lwPerimeter)
    {
        return 4 * lwPerimeter;
    }

    /*      Methods w/o overloading
    public static double doRectangleArea(double lengthArea, double widthArea)
    {
        return lengthArea * widthArea;
    }

    public static double doRectanglePerimeter(double lengthPerimeter, double widthPerimeter)
    {
        return (2 * lengthPerimeter) + (2 * widthPerimeter);
    }
    */

    // overloaded statements w/ added argument
    public static double doSquareArea(double lengthArea, double widthArea)
    {
        return lengthArea * widthArea;
    }

    // overloaded statements w/ added argument
    public static double doSquarePerimeter(double lengthPerimeter, double widthPerimeter) {
        return (2 * lengthPerimeter) + (2 * widthPerimeter);
    }

    public static double doTriangleArea(double height, double base)
    {
        return (height * base) / 2;
    }
}

Name Changer

Takes the string input of a name and does several conversions, Reverses it, Puts it in CamelCase and then converts it to a char in order to shift it to the next ASCII value

import java.util.Scanner;

public class Name_Change
{
    static String doReverse(String ToReverse)
    {
        String ReversedText = "";
        for (int count = ToReverse.length(); count > 0; count--)
        {
            ReversedText = ReversedText + ToReverse.substring(count - 1, count);
        }
        return ReversedText;
    }

    static String doCamelCase(String ToCamelCase)
    {
        String CamelCaseText = "";
        for (int count = 0; count < ToCamelCase.length(); count++)
        {
            if (count % 2 == 0)
                CamelCaseText = CamelCaseText + (ToCamelCase.substring(count, count + 1)).toUpperCase();
            else
                CamelCaseText = CamelCaseText + (ToCamelCase.substring(count, count + 1)).toLowerCase();
        }
        return CamelCaseText;
    }

    static String doShiftLetter(String toShift)
    {
        String ShiftedText = "";
        int AsciiValue;
        for (int count = 0; count < toShift.length(); count++)
        {
            AsciiValue = (int) toShift.charAt(count) + 1;
            ShiftedText += (char) AsciiValue;
        }
        return ShiftedText;
    }

    public static void main(String[] args)
    {
        String name;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter Your Name: ");
        name = input.nextLine();
        System.out.println("Name Reversed: " + doReverse(name));
        System.out.println("Name in Camel Case: " + doCamelCase(name));
        System.out.println("Name Shifted to the Next Ascii Value: " + doShiftLetter(name));
    }
}

Programming


QR Code
QR Code java_examples_2 (generated for current page)