Made with ScholarlyTurn your own files into documents like this
Shared with you
public class MyClass {
// Class code goes here
}public class MyClass {
public static void main(String[] args) {
MyClass obj = new MyClass(); // Object instantiation
}
}public class MyClass {
public void myMethod() {
// Method code goes here
}
}public class MyClass {
public void myMethod(int parameter1, String parameter2) {
// Method code goes here
}
}public class MyClass {
public static void main(String[] args) {
// Loop code goes here
}
}public class ChildClass extends ParentClass {
// Child class code goes here
}public class MyClass {
public static void main(String[] args) {
// Selection statement code goes here
}
}public class MyClass {
public static void main(String[] args) {
// Two-way selection statement code goes here
}
}// Attribute declaration template in Java [access modifier] [data type] [attribute name]; // Example private String name;
public class MyClass {
// No-argument constructor
public MyClass() {
// Constructor code here
this.var1 = var1;
var2 = 32;
}
}public class MyClass {
// Instance variables
private int myVariable;
// Parameterized constructor
public MyClass(int value) {
// Assign value to instance variable
myVariable = value;
}
}public class MyClass {
// Instance variables
private int myVariable;
// Parameterized constructor
public MyClass(int myVariable) {
// Use "this" keyword to refer to the instance variable
this.myVariable = myVariable;
}
}class Animal {
String name;
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
String breed;
Dog(String name, String breed) {
super(name); // Calling the constructor of the parent class
this.breed = breed;
}
@Override
void sound() {
super.sound(); // Calling the sound() method from the parent class
System.out.println("The dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Rex", "Labrador");
myDog.sound();
}
}// Variable declaration and initialization <datatype> <variableName> = <initialValue>; // Example: int age = 25;
// Accessor method
public <datatype> get<VariableName>() {
return <variableName>;
}
// Example:
public int getAge() {
return age;
}public class ExpressionExample {
public static void main(String[] args) {
// Writing and evaluating expressions using arithmetic operators
int a = 5;
int b = 3;
int result = a + b; // Addition
System.out.println("Addition result: " + result);
// Evaluating expressions and data types
double x = 10.0;
int y = 3;
double divisionResult = x / y; // Division
System.out.println("Division result: " + divisionResult);
// Compound assignment operators
int num = 10;
num += 5; // Equivalent to: num = num + 5
System.out.println("Compound assignment result: " + num);
// Mod operator
int modResult = a % b; // Modulus
System.out.println("Modulus result: " + modResult);
}
}// Mutator method template
public void setPropertyName(DataType propertyName) {
this.propertyName = propertyName;
}
// Example of a mutator method
public void setName(String newName) {
this.name = newName;
}// Calling a mutator method template
objectName.setPropertyName(propertyValue);
// Example of calling a mutator method
// Creating an instance of the Painter class
Painter alice = new Painter();
alice.setName("John");public class MyClass {
private int field1;
private String field2;
// Constructor
// Getter and Setter methods
// toString() method
public String toString() {
return "MyClass[field1=" + field1 + ", field2=" + field2 + "]"; //Use super.toString() to use the toStrings from the superclass
}
}// Declaration and Instantiation
int[] intArray = new int[5];
// Initialization with Values
int[] intArray = {3, 3, 5};// assigning a new value to the first element of the array intArray[0] = 10;
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}// Preconditions: the array must not be null
// Postconditions: the array will be sorted in ascending order
public void sortArray(int[] array) {
// sorting logic
}// Shape is the superclass
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
// Circle is a subclass of Shape
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}// iterating over an array using an enhanced for loop
for (int num : intArray) {
System.out.println(num);
}import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Theater {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public static void main(String[] args) {
JFrame frame = new JFrame("Theater");
Canvas canvas = new Canvas();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
drawScene(canvas);
}
private static void drawScene(Canvas canvas) {
Graphics2D g2d = (Graphics2D) canvas.getBufferStrategy().getDrawGraphics();
// Drawing operations
// Example operations:
g2d.setColor(Color.RED);
g2d.fillRect(100, 100, 50, 50); // Draws a red rectangle
g2d.setColor(Color.BLUE);
g2d.fillOval(200, 200, 50, 50); // Draws a blue oval
// Other drawing operations...
g2d.dispose();
canvas.getBufferStrategy().show();
}
private static void playSound(String soundPath) {
// Code for playing sounds
}
private static void writeNotes(String notes) {
// Code for writing notes
}
private static BufferedImage loadImage(String imagePath) {
// Code for loading and returning an image
return null;
}
}// TEMPLATE
public class MyClass {
// static variable
public static DataType variableName = value;
// static method
public static ReturnType methodName(ParameterType parameter1, ParameterType parameter2) {
// method code goes here
}
}
// EXAMPLE
public class Circle {
// static variable
public static final double PI = 3.14159;
// static method
public static double calculateArea(double radius) {
return PI * radius * radius;
}
}
sqrt(34); // square root pow(3, 4); // power - this would be like 3^4 abs(3); // absolute value // etc...
public class RoundingAndCastingExample {
public static void main(String[] args) {
// Rounding example
double originalNumber = 7.89;
long roundedNumber = Math.round(originalNumber); // roundedNumber will be 8
// Casting example
double doubleNumber = 7.89;
int intNumber = (int) doubleNumber; // intNumber will be 7
}
}int firstNumber = (int) (Math.random() * 6) + 5; // This will return a number between 11 and 5
int secondNumber = (int)(Math.random() * 10) + 1; // This will generate a number between 11 and 1
int thirdNumber = (int)(Math.random() * 30) - 5; // This will generate a number between -5 and 25double randomNumber = Math.random(); // Generates a random double value between 0.0 (inclusive) and 1.0 (exclusive)
int firstNumber = (int)(Math.random() * 6) + 5; // Between 5 and 11
int secondNumber = (int)(Math.random() * 10) + 1; // Between 1 and 11
int thirdNumber = (int)(Math.random() * 30) - 5; // Between -5 and 25
// ALIASES
String s1 = "Hello";
String s2 = s1; // s2 is an alias for s1
// EQUALITY
int x = 5;
int y = 5;
boolean areEqual = (x == y); // areEqual will be true
// OVERRIDDEN EQUALS
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MyClass myClass = (MyClass) obj;
return value == myClass.value;
}if (condition1) {
// Code block for condition1 being true
if (condition2) {
// Code block for condition1 and condition2 both being true
} else {
// Code block for condition1 being true and condition2 being false
}
} else {
// Code block for condition1 being false
}