MP4 progress
parent
e28eec5c61
commit
880852443a
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author calebfontenot
|
||||
*/
|
||||
public class CircleWithUncheckedException {
|
||||
private double radius;
|
||||
|
||||
public CircleWithUncheckedException()
|
||||
{
|
||||
this(1.0);
|
||||
}
|
||||
public CircleWithUncheckedException(double radius)
|
||||
{
|
||||
if (radius < 0) {
|
||||
throw new IllegalArgumentException("Radius cannot be negative.");
|
||||
}
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
public void setRadius(double radius) {
|
||||
if (radius < 0) {
|
||||
throw new IllegalArgumentException("Radius cannot be negative.");
|
||||
}
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CircleWithUnheckedException{" + "radius=" + radius + '}';
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println(new CircleWithUncheckedException(5));
|
||||
System.out.println(new CircleWithUncheckedException(-5));
|
||||
System.out.println(new CircleWithUncheckedException(10));
|
||||
} catch (Exception ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class TestFileClass {
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter a file path or directory: ");
|
||||
String filePath = input.nextLine();
|
||||
testFile(filePath);
|
||||
}
|
||||
public static void testFile(String filePath) {
|
||||
java.io.File file = new java.io.File(filePath);
|
||||
System.out.println("Does it exist? " + file.exists());
|
||||
System.out.println("The file has " + file.length() + " bytes.");
|
||||
System.out.println("Can it be read? " + file.canRead());
|
||||
System.out.println("Can it be written? " + file.canWrite());
|
||||
System.out.println("Is it a directory? " + file.isDirectory());
|
||||
System.out.println("Is it a file? " + file.isFile());
|
||||
System.out.println("Is it absolute? " + file.isAbsolute());
|
||||
System.out.println("Is it hidden? " + file.isHidden());
|
||||
System.out.println("The absolute path to this file is " + file.getAbsolutePath());
|
||||
System.out.println("Last modified on " + new java.util.Date(file.lastModified()));
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class ToDecimal {
|
||||
public static int hexToDecimal(String hex) {
|
||||
int decimalValue =0;
|
||||
for (int i = 0; i < hex.length(); ++i) {
|
||||
char hexChar = hex.charAt(i);
|
||||
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
|
||||
}
|
||||
return decimalValue;
|
||||
}
|
||||
public static int hexCharToDecimal(char ch) {
|
||||
ch = Character.toUpperCase(ch);
|
||||
if (ch >= 'A' && ch <= 'F') {
|
||||
return 10 + ch - 'A';
|
||||
} else if (ch >= '0' && ch <= '9'){ // ch is '0', '1', or '9'
|
||||
return ch - '0';
|
||||
} else {
|
||||
throw new NumberFormatException("Invalid hex character");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter a hex number or q/Q to quit: ");
|
||||
String hex = input.nextLine();
|
||||
while ("q".equals(hex.toLowerCase()) == false) {
|
||||
System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal(hex.toUpperCase()));
|
||||
System.out.print("Emter a hex number: ");
|
||||
hex = input.nextLine();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Triangle implements Geo{
|
||||
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;
|
||||
|
||||
public Triangle(double side1, double side2, double side3) {
|
||||
this.side1 = side1;
|
||||
this.side2 = side2;
|
||||
this.side3 = side3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
double s = (side1 + side2 + side3) / 2;
|
||||
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
|
||||
}
|
||||
@Override
|
||||
public double getPerimeter() {
|
||||
return side1 + side2 + side3;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Enter the three sides: ");
|
||||
double side1 = input.nextDouble();
|
||||
double side2 = input.nextDouble();
|
||||
double side3 = input.nextDouble();
|
||||
Triangle triangle = new Triangle(side1, side2, side3);
|
||||
System.out.println("The area is " + triangle.getArea());
|
||||
System.out.println("The perimeter is " + triangle.getPerimeter());
|
||||
System.out.println(triangle);
|
||||
}
|
||||
}
|
||||
interface Geo {
|
||||
double getArea();
|
||||
public double getPerimeter();
|
||||
}
|
Loading…
Reference in New Issue