Java Fundamentals: Arrays, Loops, Classes, and Methods
Java Arrays
In Java, arrays are a fundamental data structure used to store collections of elements of the same type. Here’s how you can declare and initialize a String array:
String[] cars = {"BMW", "Ford", "Mazda"};
You can determine the length of an array using the .length
property:
System.out.println(cars.length);
Iterate through an array using a for loop:
for (int i = 0; i < cars.length; i++) {
Multidimensional Arrays in Java
Java supports multidimensional arrays, which are essentially arrays of arrays. Here’s an example of a two-dimensional integer array:
int[][] myNumbers = {{1, 2, 3, 4}, {5, 6, 7}};
You can iterate through a multidimensional array using nested for loops:
for (int i = 0; i < myNumbers.length; i++) {
for (int j = 0; j < myNumbers[i].length; j++) {
System.out.println(myNumbers[i][j]);
}
}
Java Class Example: Speeding Ticket
Here’s an example of a Java class representing a speeding ticket:
SpeedingTicket sp1 = new SpeedingTicket(1234, 40, 20191010, 30);
System.out.println(sp1.getTicketNumber());
System.out.println(sp1.getActualSpeed());
System.out.println(sp1.getDate());
System.out.println(sp1.getSpeedLimit());
System.out.println(sp1.getOverage());
System.out.println(sp1.getFileNumber());
Switch Statements in Java
Switch statements provide a way to execute different code blocks based on the value of an expression:
switch (theMovieName) {
case "Batman":
message = "Batman is at 7:30 PM";
break;
}
Static vs. Instance Variables
Static Variables: Static variables are associated with the class itself rather than with instances of the class. Their value is the same across all instances.
Instance Variables: Instance variables are specific to each instance of a class.
Java Class Example: Movie Ticket
Here’s an example of a Java class for a movie ticket reservation system:
MovieTicket ticket = new MovieTicket();
System.out.println("Please enter your movie name: ");
ticket.setMovieName(in.nextLine());
System.out.println("Please enter ticket number: ");
ticket.setTicketNumber(in.nextInt());
System.out.println("Please enter your theater number: ");
ticket.setTheatreNumber(in.nextInt());
System.out.println("Ticket price is: " + MovieTicket.taxTotal());
ticket.setTicketPrice(in.nextInt());
String Manipulation in Java
firstName.toUpperCase()
: Converts the string to uppercase..length
: Returns the length of the string..charAt(3)
: Returns the character at index 3..substring(0, 3)
: Returns a substring from index 0 up to 3..indexOf("a")
: Returns the index of the first occurrence of “a”.
Constants in Java
public static Double Tax = 1.13;
public static final Double TAX = 3;
Array Declarations
Multidimensional Array
int[][] A = {
{1, 2, 3},
{1, 2, 3},
{4, 5, 6}
};
Single-Dimensional Array
int[] c = new int[12];
int[] c = {1, 2, 3, 4};
Java Class: Airline Reservation
This class represents an airline reservation with a first name, last name, flight number, and seat number.
/**
* This class will give a first name, last name, a flight number, and a seat number.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AirlineReservation {
private String firstName;
private String lastName;
private int flightNumber;
private int seatNumber;
private String message;
public AirlineReservation() {
}
public AirlineReservation(String firstName, String lastName, int flightNumber, int seatNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.flightNumber = flightNumber;
this.seatNumber = seatNumber;
this.message = message;
}
// Setter methods
public void setFirstName(String setFirstName) {
firstName = setFirstName;
}
public void setLastName(String setTheLastName) {
lastName = setTheLastName;
}
public void setFlightNumber(int setFlightNumber) {
flightNumber = setFlightNumber;
}
public void setSeatNumber(int setSeatNumber) {
seatNumber = setSeatNumber;
}
public void setMessage(String setMessage) {
message = setMessage;
}
// Getter methods
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getFlightNumber() {
return flightNumber;
}
public int getSeatNumber() {
return seatNumber;
}
public String getMessage() {
return message;
}
public String getSeatClass() {
switch (seatNumber) {
case 1:
case 2:
case 3:
message = "First Class";
break;
case 4:
case 5:
case 6:
message = "Second Class";
break;
case 7:
case 8:
case 9:
message = "Third Class";
break;
case 10:
case 11:
case 12:
message = "Fourth Class";
break;
}
return message;
}
public String upperCaseName() {
for (int i = 0; i < firstName.length(); i++) {
System.out.println(firstName.toUpperCase().charAt(i));
}
return firstName.toUpperCase();
}
}