0% found this document useful (0 votes)
5 views9 pages

Capitulo 8

The document contains Java code examples demonstrating various programming concepts such as class creation, method overloading, exception handling, and the use of enums. It includes classes for managing time, employee data, and mathematical operations, along with tests to validate their functionality. Additionally, it showcases the use of static imports and package data manipulation.

Uploaded by

fert125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Capitulo 8

The document contains Java code examples demonstrating various programming concepts such as class creation, method overloading, exception handling, and the use of enums. It includes classes for managing time, employee data, and mathematical operations, along with tests to validate their functionality. Additionally, it showcases the use of static imports and package data manipulation.

Uploaded by

fert125
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CAPITULO 8

CLASE TIEMPO

public class Time1 {


private int hour;
private int minute;
private int second;

public void setTime(int hour, int minute, int second) {


if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60
|| second < 0 || second >= 60) {
throw new IllegalArgumentException("hour, minute
and/or second was out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}

public String toUniversalString() {


return String.format("%02d:%02d:%02d", hour, minute,
second);
}

public String toString() {


return String.format("%d:%02d:%02d %s",
((hour == 0 || hour == 12) ? 12 : hour % 12),
minute, second, (hour < 12 ? "AM" : "PM"));
}
}

PRUEBA CLASE TIEMPO

public class Time1Test {


public static void main(String[] args) {
Time1 time = new Time1();

displayTime("After time object is created", time);


System.out.println();

time.setTime(13, 27, 6);


displayTime("After calling setTime", time);
System.out.println();

try {
time.setTime(99, 99, 99);
} catch (IllegalArgumentException e) {
System.out.printf("Exception: %s%n%n",
e.getMessage());
}
displayTime("After calling setTime with invalid values",
time);
}

private static void displayTime(String header, Time1 t) {


System.out.printf("%s%nUniversal time: %s%nStandard time:
%s%n",
header, t.toUniversalString(), t.toString());
}
}

INTENTO DE ACCEDER A METODOS PRIVADOS

public class MemberAccessTest {


public static void main(String[] args) {
Time1 time = new Time1();

time.hour = 7;
time.minute = 15;
time.second = 30;
}
}

USO DE THIS

public class ThisTest {


public static void main(String[] args) {
SimpleTime time = new SimpleTime(15, 30, 19);
System.out.println(time.buildString());
}
}

class SimpleTime {
private int hour;
private int minute;
private int second;

public SimpleTime(int hour, int minute, int second) {


this.hour = hour;
this.minute = minute;
this.second = second;
}

public String buildString() {


return String.format("%24s: %s%n%24s: %s",
"this.toUniversalString()",
this.toUniversalString(),
"toUniversalString()", toUniversalString());
}

public String toUniversalString() {


return String.format("%02d:%02d:%02d", this.hour,
this.minute, this.second);
}
}

SOBRECARGA DE CONSTRUCTORES

Time2 t1 = new Time2(); // 00:00:00


Time2 t2 = new Time2(13); // 13:00:00
Time2 t3 = new Time2(13, 45); // 13:45:00
Time2 t4 = new Time2(13, 45, 30); // 13:45:30
Time2 t5 = new Time2(t4); // copia de t4

public class Time2 {


private int hour;
private int minute;
private int second;

public Time2() {
this(0, 0, 0);
}

public Time2(int hour) {


this(hour, 0, 0);
}

public Time2(int hour, int minute) {


this(hour, minute, 0);
}

public Time2(int hour, int minute, int second) {


if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-
23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be
0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be
0-59");

this.hour = hour;
this.minute = minute;
this.second = second;
}

public Time2(Time2 time) {


this(time.getHour(), time.getMinute(), time.getSecond());
}

public void setTime(int hour, int minute, int second) {


if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-
23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be
0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be
0-59");

this.hour = hour;
this.minute = minute;
this.second = second;
}

public void setHour(int hour) {


if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-
23");
this.hour = hour;
}

public void setMinute(int minute) {


if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be
0-59");
this.minute = minute;
}

public void setSecond(int second) {


if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be
0-59");
this.second = second;
}

public int getHour() {


return hour;
}

public int getMinute() {


return minute;
}

public int getSecond() {


return second;
}

public String toUniversalString() {


return String.format("%02d:%02d:%02d", getHour(),
getMinute(), getSecond());
}

public String toString() {


return String.format("%d:%02d:%02d %s",
((getHour() == 0 || getHour() == 12) ? 12 :
getHour() % 12),
getMinute(), getSecond(), (getHour() < 12 ?
"AM" : "PM"));
}
}

TEST FECHA

public class Time2Test {


public static void main(String[] args) {
Time2 t1 = new Time2();
Time2 t2 = new Time2(2);
Time2 t3 = new Time2(21, 34);
Time2 t4 = new Time2(12, 25, 42);
Time2 t5 = new Time2(t4);

System.out.println("Constructed with:");
displayTime("t1: all default arguments", t1);
displayTime("t2: hour specified; default minute and
second", t2);
displayTime("t3: hour and minute specified; default
second", t3);
displayTime("t4: hour, minute and second specified", t4);
displayTime("t5: Time2 object t4 specified", t5);

try {
Time2 t6 = new Time2(27, 74, 99);
} catch (IllegalArgumentException e) {
System.out.printf("%nException while initializing
t6: %s%n", e.getMessage());
}
}

private static void displayTime(String header, Time2 t) {


System.out.printf("%s%n %s%n %s%n", header,
t.toUniversalString(), t.toString());
}
}

ANÁLISIS DEL EJEMPLO EMPLEADO

declaracion de la clase

public class Employee {


private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;

public Employee(String firstName, String lastName, Date


birthDate, Date hireDate) {
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.hireDate = hireDate;
}

public String toString() {


return String.format("%s, %s Hired: %s Birthday: %s",
lastName, firstName, hireDate, birthDate);
}
}

prueba de la clase

public class EmployeeTest {


public static void main(String[] args) {
Date birth = new Date(7, 24, 1949);
Date hire = new Date(3, 12, 1988);
Employee employee = new Employee("Bob", "Blue", birth,
hire);

System.out.println(employee);
}
}

LIBRO JAVA USANDO ENUM

public enum Book {


JHTP("Java How to Program", "2015"),
CHTP("C How to Program", "2013"),
IW3HTP("Internet & World Wide Web How to Program", "2012"),
CPPHTP("C++ How to Program", "2014"),
VBHTP("Visual Basic How to Program", "2014"),
CSHARPHTP("Visual C# How to Program", "2014");

private final String title;


private final String copyrightYear;

Book(String title, String copyrightYear) {


this.title = title;
this.copyrightYear = copyrightYear;
}

public String getTitle() {


return title;
}

public String getCopyrightYear() {


return copyrightYear;
}
}

PRUEBA ENUM

import java.util.EnumSet;
public class EnumTest {
public static void main(String[] args) {
System.out.println("All books:");

for (Book book : Book.values())


System.out.printf("%-10s%-45s%s%n", book,
book.getTitle(), book.getCopyrightYear());

System.out.printf("%nDisplay a range of enum constants:


%n");

for (Book book : EnumSet.range(Book.JHTP, Book.CPPHTP))


System.out.printf("%-10s%-45s%s%n", book,
book.getTitle(), book.getCopyrightYear());
}
}

EMPLEADO OTRA VEZ

public class Employee1 {


private static int count = 0;
private String firstName;
private String lastName;

public Employee1(String firstName, String lastName) {


this.firstName = firstName;
this.lastName = lastName;

++count;
System.out.printf("Employee constructor: %s %s; count =
%d%n",
firstName, lastName, count);
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public static int getCount() {


return count;
}
}

PRUEBA EMPLEADO

public class EmployeeTest1 {


public static void main(String[] args) {
System.out.printf("Employees before instantiation:%d%n",
Employee1.getCount());
Employee1 e1 = new Employee1("Susan", "Baker");
Employee1 e2 = new Employee1("Bob", "Blue");

System.out.printf("%nEmployees after instantiation:%n");


System.out.printf("via e1.getCount(): %d%n",
e1.getCount());
System.out.printf("via e2.getCount(): %d%n",
e2.getCount());
System.out.printf("via Employee.getCount(): %d%n",
Employee1.getCount());

System.out.printf("%nEmployee 1: %s %s%nEmployee 2: %s %s
%n",
e1.getFirstName(), e1.getLastName(),
e2.getFirstName(), e2.getLastName());
}
}

IMPORTACIÓN ESTÁTICA

public class Matematica {


public static final double PI = 3.14159;
public static int cuadrado(int x) {
return x * x;
}
}

import static Matematica.PI;


import static Matematica.cuadrado;

int r = 3;
double area = PI * cuadrado(r);

import static java.lang.Math.*;

double x = sqrt(25); // en lugar de Math.sqrt(25)


double y = pow(2, 3); // en lugar de Math.pow(2, 3)

PAQUETES

public class PackageDataTest {


public static void main(String[] args) {
PackageData packageData = new PackageData();

System.out.printf("After instantiation:%n%s%n",
packageData);

packageData.number = 77;
packageData.string = "Goodbye";

System.out.printf("%nAfter changing values:%n%s%n",


packageData);
}
}

class PackageData {
int number;
String string;

public PackageData() {
number = 0;
string = "Hello";
}

public String toString() {


return String.format("number: %d; string: %s", number,
string);
}
}

INTERESES

import java.math.BigDecimal;
import java.text.NumberFormat;

public class Interest {


public static void main(String[] args) {
BigDecimal principal = BigDecimal.valueOf(1000.0);
BigDecimal rate = BigDecimal.valueOf(0.05);

System.out.printf("%s%20s%n", "Year", "Amount on


deposit");

for (int year = 1; year <= 10; year++) {


BigDecimal amount =
principal.multiply(rate.add(BigDecimal.ONE).pow(year));
System.out.printf("%4d%20s%n", year,
NumberFormat.getCurrencyInstance().format(amount));
}
}
}

You might also like