public class MyFirstClass {
}
House myHouse = new House();
House friendsHouse = new House();
House parentsHouse = new House();
fido.bark(3);
fido = name of object
. = dot operator, used to call a method in a class
bark = name of method
3 = argument, a value passed to a method when the method is called
The key terms in HelloStatic introduction:
// Define Static Method within a Class
public class HelloStatic {
// Java standard runtime entry point
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
// A method call allows us to execute code that is wrapped in Class
HelloStatic.main(null); // Class prefix allows reference of Static Method
The key terms in HelloStatic introduction:
// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
// instance variable have access modifier (private is most common), data type, and name
private String hello;
// constructor signature 1, public and zero arguments, constructors do not have return type
public HelloDynamic() { // 0 argument constructor
this.setHello("Hello, World!"); // using setter with static string
}
// constructor signature, public and one argument
public HelloDynamic(String hello) { // 1 argument constructor
this.setHello(hello); // using setter with local variable passed into constructor
}
// setter/mutator, setter have void return type and a parameter
public void setHello(String hello) { // setter
this.hello = hello; // instance variable on the left, local variable on the right
}
// getter/accessor, getter used to return private instance variable (encapsulated), return type is String
public String getHello() { // getter
return this.hello;
}
// public static void main(String[] args) is signature for main/drivers/tester method
// a driver/tester method is singular or called a class method, it is never part of an object
public static void main(String[] args) {
HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
System.out.println(hd1.getHello()); // accessing getter
System.out.println(hd2.getHello());
}
}
// IJava activation
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!
Building Java code that contains instance of class, constructors, parameters, setters
// Class definition
class GroceryItem {
// Instance variables
private String name;
private int quantity;
// Constructor with parameters
public GroceryItem(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
// Getter for name
public String getName() {
return name;
}
// Setter for quantity
public void setQuantity(int quantity) {
if (quantity >= 0) {
this.quantity = quantity;
} else {
System.out.println("Quantity cannot be negative.");
}
}
public static void main(String[] args) {
// Creating instances of the GroceryItem class
GroceryItem item1 = new GroceryItem("apples", 5); // Constructor with arguments
GroceryItem item2 = new GroceryItem("breads", 2);
System.out.println("Grace needs to buy " + item1.quantity + " " + item1.name);
System.out.println("Grace needs to buy " + item2.quantity + " " + item2.name);
}
}
GroceryItem.main(null);
Grace needs to buy 5 apples
Grace needs to buy 2 breads
// Defining Location class
class MyLocation {
// creating instance variables
private int house;
private String street;
private String city;
private String state;
private int zip;
//Class constructor with parameters
public MyLocation(int house, String street, String city, String state, int zip) {
this.house = house;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
// Getter for house
public int getHouse() {
return house;
}
// Getter for street
public String getStreet() {
return street;
}
// Getter for city
public String getCity() {
return city;
}
// Getter for house
public String getState() {
return state;
}
// Getter for house
public int getZip() {
return zip;
}
public static void main(String[] args) {
// Creating instances of the GroceryItem class
MyLocation location1 = new MyLocation(16601, "Nighthawk Lane", "San Diego", "California", 92127); // Constructor with arguments
MyLocation location2 = new MyLocation(17170, "Camino Del Sur", "San Diego", "California", 92127);
System.out.println("Grace was at " + location1.house + " " + location1.street + " " + location1.city + ", " + location1.state + " " + location1.zip);
System.out.println("Grace was at " + location2.house + " " + location2.street + " " + location2.city + ", " + location2.state + " " + location2.zip);
}
}
MyLocation.main(null);
Grace was at 16601 Nighthawk Lane San Diego, California 92127
Grace was at 17170 Camino Del Sur San Diego, California 92127
public class StringUtils {
public static String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}
public static void main(String[] args) {
String original = "Hello, World!";
String reversed = StringUtils.reverse(original);
System.out.println("Original: " + original);
System.out.println("Reversed: " + reversed);
}
}
StringUtils.main(null);
Original: Hello, World!
Reversed: !dlroW ,olleH
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int lowerBound = 1;
int upperBound = 100;
int secretNumber = random.nextInt(upperBound - lowerBound + 1) + lowerBound;
System.out.println("Welcome to the Number Guessing Game!");
System.out.println("I have selected a number between " + lowerBound + " and " + upperBound + ". Try to guess it!");
int attempts = 0;
int guess = 0;
while (guess != secretNumber) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < secretNumber) {
System.out.println("Too low! Try again.");
} else if (guess > secretNumber) {
System.out.println("Too high! Try again.");
}
}
System.out.println("Congratulations! You've guessed the number " + secretNumber + " in " + attempts + " attempts.");
scanner.close();
}
}
NumberGuessingGame.main(null);
Welcome to the Number Guessing Game!
I have selected a number between 1 and 100. Try to guess it!
Enter your guess: Too low! Try again.
Enter your guess: Too low! Try again.
Enter your guess: Too low! Try again.
Enter your guess: Too high! Try again.
Enter your guess: Too high! Try again.
Enter your guess: Too high! Try again.
Enter your guess: Too low! Try again.
Enter your guess: Congratulations! You've guessed the number 84 in 8 attempts.