askIT

Java Class

Lagre Download example files

When your program gets complicated you can split it into several files. A file in Java is called a class.

Below is an example of a program that will represent our solar system. I first make a new Java project called SolarSystem.

03_velg_java_application.jpg

04_prosjektnavn_solsystem.jpg

Create class
To create an additional class in Java, right-click one of the class name under SourcePackages and select New -> Java Class...

The image below describes how to create a new class named Planet.

ny_klasse.gif

klassenavn.gif

temperatur_klasser.gif

Planet.java -> Conditions
Open Planet.java and enter the states a planet may have.

// States
String name;
double orbitalPeriod;
double radius;
double distanceFromSun;
int naturalSatellites;

Planet.java -> Constructor
We also creates a constructor that sets the different states that the object Planet may have.

// Constructor
public Planet(String inpName, double inpOrbitalPeriod, double inpRadius, double inpDistanceFromSun, int inpNaturalSatellites) {
        name = inpName;
        orbitalPeriod = inpOrbitalPeriod;
        radius = inpRadius;
        distanceFromSun = inpDistanceFromSun;
        naturalSatellites = inpNaturalSatellites;
}

Planet.java -> Set and Get for all variables
We must create set and get for all of the variables:
name, orbitalPeriod, radius, distanceFromSun, naturalSatellites.
These we use to set variable values and retrieve the values.

// Name set and get
public String setName(String value){
        return name = value;
}
public String getName(){
        return name;
}

// Orbital Period set and get
public double setOrbitalPeriod(double value){
        return orbitalPeriod = value;
}
public double getOrbitalPeriod(){
        return orbitalPeriod;
}

// Radius set and get
public double setRadius(double value){
        return radius = value;
}
public double getRadius(){
        return radius;
}

// Distance From Sun set and get
public double setDistanceFromSun(double value){
        return distanceFromSun = value;
}
public double getDistanceFromSun(){
        return distanceFromSun;
}

// Natural Satellites set and get
public int setNaturalSatellites(int value){
        return naturalSatellites = value;
}
public int getNaturalSatellites(){
        return naturalSatellites;
}

Solsystem.java -> Create planets
We can create planets in the class Solsystem.java. This is done by typing
Planet name = new Planet("name", orbital period around the Sun, radius around the equator, distance to the sun, number of satellites);

As an example, this code to create planet Mercury:
Planet mercury = new Planet("Mercury", 57909176, 24397, 57909176, 0);

package solarsystem;
public class SolarSystem {

        public static void main(String[] args) {
               
                // Create planets
                // "Name", Orbital period around the Sun, Radius around the equator, distance to the sun, Number of satellites
                Planet sun = new Planet("Sun", 0, 695500, 0, 0);
                Planet merkur = new Planet("Mercury", 87.97, 2439.7, 57909176, 0);
                Planet venus = new Planet("Venus", 224.70, 6051.5, 108200000, 0);
                Planet earth = new Planet("Earth", 365.25, 6378.14, 149600000, 1);
                Planet mars = new Planet("Mars", 686.97, 3390, 227939100, 2);
                Planet jupiter = new Planet("Jupiter", 4332.59, 71492, 778500000, 67);
                Planet saturn = new Planet("Saturn", 10759.22, 60268, 1433449370, 62);
                Planet uranus = new Planet("Uranus", 30799.09, 25559, 2870972200l, 27);
                Planet neptun = new Planet("Neptun", 60.19, 49528, 4503443661l, 13);
               
        }
}

Solsystem.java -> Printing planets, and change planets
To print information such as. name of a planet, use code
objekt.getName();.

To change a planet's information you can use code
objekt.setName();.

// Print objects
System.out.println("The solar system consists of:");
System.out.println("--------");
System.out.println("Name: " + sun.getName());
System.out.println("Orbital period around the sun: " + sun.getDistanceFromSun());
System.out.println("Radius around the equator: " + sun.getRadius());
System.out.println("Distance to the Sun: " + sun.getDistanceFromSun());
System.out.println("Number of satellites: " + sun.getNaturalSatellites());
               
System.out.println("--------");
System.out.println("Name: " + mercury.getName());
System.out.println("Orbital period around the sun: " + mercury.getDistanceFromSun());
System.out.println("Radius around the equator: " + mercury.getRadius());
System.out.println("Distance to the Sun: " + mercury.getDistanceFromSun());
System.out.println("Number of satellites: " + mercury.getNaturalSatellites());

Assignments

1. Create a program that lets you create employees using a class employee. Employees shall have the first name, surname, date of birth and occupation. Let the user add, modify and delete employees.

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.