Objects
popcorn hacks for objects
class Dog {
public String name;
public String breed;
public int age;
public Dog(String name, String breed, int age){
this.name = name;
this.breed = breed;
this.age = age;
}
public void bark(){
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Shelby", "Golden Retriever", 5); // name, breed, age
myDog.bark(); // should print "Woof!"
}
}
Main.main(null);
Woof!
class Movie{
public String title;
public Movie(String title){
this.title = title;
}
public void printTitle(){
System.out.println(this.title);
}
}
Movie myMovie = new Movie("Star Wars");
myMovie.printTitle();
Star Wars
public class Main {
public static void main(String[] args) {
Integer num1 = 50;
Integer num2 = new Integer(75);
Double d1 = 3.14;
double d2 = new Double(2.718);
System.out.println("Sum of integers: " + (num1 + num2));
System.out.println("Product of doubles: " + (d1 * d2));
}
}
Main.main(null);
Sum of integers: 125
Product of doubles: 8.53452
public class Main {
public static void main(String[] args) {
// TODO: Create an Integer object using autoboxing
Integer myInteger = Integer.valueOf(123);
// TODO: Create a double primitive from a Double object (unboxing)
double myDouble = (new Double(123.45)).doubleValue();
// TODO: Print both values
System.out.println(myInteger);
System.out.println(myDouble);
}
}
Main.main(null);
123
123.45
import java.util.*;
public class Main {
public static double randomize(double a, double b){
return Math.random() * (b-a) + a;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a = scan.nextDouble();
double b = scan.nextDouble();
System.out.println("a: "+(new Double(a)).toString());
System.out.println("b: "+(new Double(b)).toString());
System.out.println(randomize(a, b));
}
}
Main.main(null);
a: 2.0
b: 5.0
3.5490194837027325
public class Circle {
public double radius;
public Circle(double radius){
this.radius = radius;
}
public double circumference(){
return Math.PI * this.radius * 2;
}
public double area(){
return Math.PI * Math.pow(this.radius,2) ;
}
}
public class Student {
public String name;
public Integer grade;
// 1. Class variables: name (String) and grade (Integer)
public Student(String name, Integer grade){
this.name = name;
this.grade = grade;
}
public Student(String name, int grade){
this.name = name;
this.grade = new Integer(grade);
}
// 2. Constructor to initialize name and grade
public int nameLength(){
return this.name.length();
}
// 3. nameLength() method: Return the length of the student's name
public Double getGradeAsDouble(){
return new Double (this.grade.doubleValue());
}
// 4. getGradeAsDouble() method: Return the grade as the Double wrapper type
public double getScaledGrade(){
return this.grade.doubleValue() / 2;
}
// 5. getScaledGrade() method: Return grade divided by 2
}
public class Main {
public static void main(String[] args) {
// Testing the Circle class
Circle circle1 = new Circle(5.0);
Circle circle2 = new Circle(7.0);
System.out.println("Circle 1:");
System.out.println("Radius: " + circle1.radius);
System.out.println("Circumference: " + circle1.circumference());
System.out.println("Area: " + circle1.area());
System.out.println("\nCircle 2:");
System.out.println("Radius: " + circle2.radius);
System.out.println("Circumference: " + circle2.circumference());
System.out.println("Area: " + circle2.area());
// Testing the Student class
Student student1 = new Student("Aadit", 75);
Student student2 = new Student("Emily", 45);
System.out.println("\nStudent 1:");
System.out.println("Name: " + student1.name);
System.out.println("Name Length: " + student1.nameLength());
System.out.println("Grade: " + student1.getGradeAsDouble());
System.out.println("Scaled Grade: " + student1.getScaledGrade());
System.out.println("\nStudent 2:");
System.out.println("Name: " + student2.name);
System.out.println("Name Length: " + student2.nameLength());
System.out.println("Grade: " + student2.getGradeAsDouble());
System.out.println("Scaled Grade: " + student2.getScaledGrade());
}
}
Main.main(null);
Circle 1:
Radius: 5.0
Circumference: 31.41592653589793
Area: 78.53981633974483
Circle 2:
Radius: 7.0
Circumference: 43.982297150257104
Area: 153.93804002589985
Student 1:
Name: Aadit
Name Length: 5
Grade: 75.0
Scaled Grade: 37.5
Student 2:
Name: Emily
Name Length: 5
Grade: 45.0
Scaled Grade: 22.5