This commit is contained in:
2023-02-09 12:55:55 -06:00
parent 697b231cc7
commit 6ce8c0d880
26 changed files with 427 additions and 2 deletions

View File

@@ -0,0 +1,146 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.lab3_calebfontenot;
import java.util.Date;
import java.util.Objects;
/**
*
* @author caleb
*/
public class Account {
public Account()
{
dateCreated = new Date();
}
public Account(double balance) {
this.balance = balance;
dateCreated = new Date();
}
@Override
public String toString()
{
return "Account{" + "id=" + id + ", balance=" + balance + ", dateCreated=" + dateCreated + '}';
}
private int id;
private double balance;
private static int annualInterestRate;
private Date dateCreated;
/**
* Get the value of id
*
* @return the value of id
*/
public int getId()
{
return id;
}
/**
* Set the value of id
*
* @param id new value of id
*/
public void setId(int id)
{
this.id = id;
}
/**
* Get the value of balance
*
* @return the value of balance
*/
public double getBalance()
{
return balance;
}
/**
* Set the value of balance
*
* @param balance new value of balance
*/
public void setBalance(double balance)
{
this.balance = balance;
}
/**
* Get the value of annualInterestRate
*
* @return the value of annualInterestRate
*/
public static int getAnnualInterestRate()
{
return annualInterestRate;
}
/**
* Set the value of annualInterestRate
*
* @param annualInterestRate new value of annualInterestRate
*/
public static void setAnnualInterestRate(int annualInterestRate)
{
Account.annualInterestRate = annualInterestRate;
}
public static void main(String[] args)
{
Account account1 = new Account();
Account account2 = new Account(100);
System.out.println(account1);
System.out.println(account2);
System.out.println(account1.equals(account2));
System.out.println(account1.equals(new A()));
}
public void withdraw(double amount) {
this.balance -= amount;
}
// public void withdraw(double amount) {
// this.balance -= amount;
// }
@Override
public int hashCode()
{
int hash = 3;
return hash;
}
@Override
public boolean equals(Object obj)
{
// If the parameter obj is the same as this object
if (this == obj) { // compare addresses
return true;
}
if (obj == null) { // parameter is null
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
// here we are certain
final Account other = (Account) obj;
if (this.id != other.id) {
return false;
}
if (Double.doubleToLongBits(this.balance) != Double.doubleToLongBits(other.balance)) {
return false;
}
return Objects.equals(this.dateCreated, other.dateCreated);
}
}class A {}

View File

@@ -0,0 +1,53 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.lab3_calebfontenot;
/**
*
* @author caleb
*/
public class Employee {
private String name;
public Employee () {
}
public Employee(String name)
{
this.name = name;
}
/**
* Get the value of name
*
* @return the value of name
*/
public String getName()
{
return name;
}
/**
* Set the value of name
*
* @param name new value of name
*/
public void setName(String name)
{
this.name = name;
}
public static void main(String[] args)
{
Employee e = new Employee();
e.setName("John Wayne");
System.out.println(e.getName());
Employee e1 = new Employee("Mary Poppins");
System.out.println(e1.getName());
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.calebfontenot.lab3_calebfontenot;
/**
*
* @author caleb
*/
public class Lab3_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,85 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.lab3_calebfontenot;
/**
*
* @author caleb
*/
public class MyInteger {
private int value;
/**
* Get the value of value
*
* @return the value of value
*/
public int getValue()
{
return this.value;
}
@Override
public int hashCode()
{
int hash = 7;
return hash;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MyInteger other = (MyInteger) obj;
return this.value == other.value;
}
@Override
public String toString()
{
return "MyInteger{" + "value=" + value + '}';
}
public MyInteger(int value)
{
this.value = value;
}
public static boolean isEven(MyInteger other) {
return other.getValue() % 2 == 0;
}
public static boolean isPrime(MyInteger other) {
int numToTest = other.getValue();
for (int i = 2; i <= numToTest / 2; ++i) {
if (numToTest % i == 0) {
return false;
}
}
return true;
}
public static int parseInteger(char[] array) {
String s = "";
for (char i: array) {
s += i;
}
return Integer.parseInt(String.valueOf(s));
}
public static void main(String[] args)
{
System.out.println(parseInteger(new char[] {'7', '6'}));
System.out.println(MyInteger.isPrime(new MyInteger(3)));
System.out.println(MyInteger.isEven(new MyInteger(4)));
System.out.println(MyInteger.isEven(new MyInteger(3)));
}
}

View File

@@ -0,0 +1,151 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package jan17;
import java.util.Date;
import java.util.Random;
/**
*
* @author caleb
*/
public class Circle {
static int numberOfObjectsRunning;
double radius;
public int forEverybody = 0;
public Circle()
{
this(0);
numberOfObjectsRunning++;
}
public Circle(double radius)
{
numberOfObjectsRunning++;
this.radius = radius;
this.dateOfCreation = new Date();
}
/**
* Get the value of radius
*
* @return the value of radius
*/
public double getRadius()
{
return radius;
}
/**
* Set the value of radius
*
* @param radius new value of radius
*/
public void setRadius(double radius)
{
this.radius = radius;
}
private Date dateOfCreation;
/**
* Get the value of dateOfCreation
*
* @return the value of dateOfCreation
*/
public Date getDateOfCreation()
{
return dateOfCreation;
}
public Circle getCircle() {
return this;
}
/**
* Set the value of dateOfCreation
*
* @param dateOfCreation new value of dateOfCreation
*/
public void setDateOfCreation(Date dateOfCreation)
{
this.dateOfCreation = dateOfCreation;
}
public double getArea() {
return Math.PI * (this.radius * this.radius);
}
@Override
public String toString()
{
return "Circle{" + "radius=" + radius + ", dateOfCreation=" + dateOfCreation + '}';
}
public static void main(String[] args)
{
// new Circle objects
System.out.println("Number of objects: " + Circle.numberOfObjectsRunning);
Circle c1 = new Circle();
System.out.println("Number of objects: " + Circle.numberOfObjectsRunning);
Circle c2 = new Circle(2.0);
System.out.println("Number of objects: " + Circle.numberOfObjectsRunning);
// two ways to set the radius
c1.radius = 10.0;
c1.setRadius(10.0);
// two ways to get the radius
System.out.println("radius of c1: " + c1.radius);
System.out.println("radius of c1: " + c1.getRadius());
System.out.println("radius of c1: " + c2.radius);
System.out.println("radius of c1: " + c2.getRadius());
// use object's methods
System.out.println("area of c1: " + c1.getArea());
System.out.println("area of c2: " + c2.getArea());
System.out.println(c1.getDateOfCreation());
c1.setDateOfCreation(new Date());
System.out.println(c1.getDateOfCreation());
Random rng = new Random();
System.out.println(rng.nextDouble());
System.out.println(new java.util.Random().nextInt());
System.out.println(c1);
System.out.println(c1.toString());
System.out.println();
printMultiples(c1, 4);
System.out.println();
Circle[] ar = new Circle[3];
ar[0] = c1;
ar[1] = new Circle(10);
ar[2] = new Circle(3.8);
Circle.printArray(ar);
System.out.println(c1.getCircle());
}
public static void printArray(Circle[] ar)
{
for (Circle ar1: ar) {
System.out.println(ar1);
}
}
public static void printMultiples(Circle c, int howManyTimes) {
for (int i = 0; i < howManyTimes; ++i) {
System.out.println(c);
}
}
}

View File

@@ -0,0 +1,18 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package jan17;
/**
*
* @author caleb
*/
public class TestCircle {
public static void main(String[] args)
{
Circle c1 = new Circle();
System.out.println(c1.radius);
System.out.println(c1);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package jan17;
/**
*
* @author caleb
*/
public class TestImmutable {
public static void main(String[] args)
{
Circle c = new Circle(10);
System.out.println(c);
Circle c1 = c.getCircle();
c1.getDateOfCreation().setTime(1234567);
System.out.println(c1);
}
}

View File

@@ -0,0 +1,19 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package jan17.x;
import jan17.Circle;
/**
*
* @author caleb
*/
public class TestPublic {
public static void main(String[] args)
{
Circle c1 = new Circle();
System.out.println(c1.forEverybody);
}
}