NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Fig: 8.7
##Date
public class Date {

private int month;
private int day;
private int year;

private static final int[] daysPerMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public Date(int month, int day, int year) {
if (month <= 0 || month > 12) {
throw new IllegalArgumentException("month (" + month + ") must be 1-12");
}

if (day <= 0 || (day > daysPerMonth[month] && !(month == 2 && day == 29))) {
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}

if (month == 2 && day == 29 && !(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) {
throw new IllegalArgumentException("day (" + day + ") out-of-range for the specified month and year");
}

this.month = month;
this.day = day;
this.year = year;
System.out.printf("Date object constructor for date %s%n", this);
}

public String toString() {

return String.format("%d/%d/%d", month, day, year);
}
}



Fig:8.8
##Employee
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);

}
}



Fig:8.9
##EmployeeTest
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);
}
}



Fig:9.4
##CommissionEmployee extends Object
public class CommissionEmployee extends Object {

private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales;
private double commissionRate;

public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate) {
if (grossSales < 0.0) {
throw new IllegalArgumentException("Gross sales must be >= 0.0");
}

if (commissionRate <= 0.0 || commissionRate >= 1.0) {
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");
}

this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getSocialSecurityNumber() {
return socialSecurityNumber;
}

public void setGrossSales(double grossSales) {
if (grossSales < 0.0) {
throw new IllegalArgumentException("Gross sales must be >= 0.0");
}

this.grossSales = grossSales;
}

public double getGrossSales() {
return grossSales;
}

public void setCommissionRate(double commissionRate) {
if (commissionRate <= 0.0 || commissionRate >= 1.0) {
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");
}

this.commissionRate = commissionRate;
}

public double getCommissionRate() {
return commissionRate;
}

public double earnings() {
return commissionRate * grossSales;
}

@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate);

}
}



Fig:9.5
##CommissionEmployeeTest
public class CommissionEmployeeTest {

public static void main(String[] args) {
CommissionEmployee employee = new CommissionEmployee(
"Sue", "Jones", "222-22-2222", 10000, .06);
System.out.println("Employee information obtained by get methods:");
System.out.printf("%n%s %s%n", "First name is", employee.getFirstName());
System.out.printf("%s %s%n", "Last name is", employee.getLastName());
System.out.printf("%s %s%n", "Social security number is", employee.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is", employee.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is", employee.getCommissionRate());

employee.setGrossSales(5000);
employee.setCommissionRate(.1);

System.out.printf("%n%s:%n%n %n",
"Updated employee information obtained by toString", employee);
}
}



Fig:9.6
##BasePlusCommissionEmployee
public class BasePlusCommissionEmployee
{
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales;
private double commissionRate;
private double baseSalary;

public BasePlusCommissionEmployee(String firstName, String lastName,String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary)
{

if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");

if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");

if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be >= 0.0");

this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.grossSales = grossSales;
this.commissionRate = commissionRate;
this.baseSalary = baseSalary;
}
public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getSocialSecurityNumber() {
return socialSecurityNumber;
}

public void setGrossSales(double grossSales) {
if (grossSales < 0.0)
throw new IllegalArgumentException("Gross sales must be >= 0.0");

this.grossSales = grossSales;
}

public double getGrossSales() {
return grossSales;
}

public void setCommissionRate(double commissionRate) {
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");

this.commissionRate = commissionRate;
}
public double getCommissionRate() {
return commissionRate;
}

public void setBaseSalary(double baseSalary) {
if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be >= 0.0");

this.baseSalary = baseSalary;
}

public double getBaseSalary() {
return baseSalary;
}

public double earnings() {
return getBaseSalary() + (commissionRate * grossSales);
}

@Override
public String toString() {
return String.format(
"%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f%n%s: %.2f",
"base-salaried commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales, "commission rate", commissionRate,
"base salary", baseSalary);
}
}



Fig:9.7
##BasePlusCommissionEmployeeTest
public class BasePlusCommissionEmployeeTest {

public static void main(String[] args) {
BasePlusCommissionEmployee employee
= new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);
System.out.println("Employee information obtained by get methods:%n");
System.out.printf("%s %s%n", "First name is",
employee.getFirstName());
System.out.printf("%s %s%n", "Last name is",
employee.getLastName());
System.out.printf("%s %s%n", "Social security number is",
employee.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is",
employee.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is",
employee.getCommissionRate());
System.out.printf("%s %.2f%n", "Base salary is",
employee.getBaseSalary());

employee.setBaseSalary(1000);
System.out.printf("%n%s:%n%n%s%n",
"Updated employee information obtained by toString",
employee.toString());
}
}




Fig:9.8
##BasePlusCommissionEmployee extends CommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;

public BasePlusCommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary) {
super(firstName, lastName, socialSecurityNumber, grossSales, commissionRate);
if (baseSalary < 0.0)
throw new IllegalArgumentException("Base salary must be >= 0.0");
this.baseSalary = baseSalary;
}
public void setBaseSalary(double baseSalary) {
if (baseSalary < 0.0) {
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
}
this.baseSalary = baseSalary;
}

public double getBaseSalary() {
return baseSalary;
}

@Override
public double earnings() {
return baseSalary + (commissionRate * grossSales);
}

@Override
public String toString()
{
return String.format(
"%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f%n%s: %.2f",
"base-salaried commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales, "commission rate", commissionRate,
"base salary", baseSalary);
}
}



Fig: 910
##CommissionEmployee
public class CommissionEmployee
{
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales;
private double commissionRate;
public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate) {
if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getSocialSecurityNumber() {
return socialSecurityNumber;
}

public void setGrossSales(double grossSales) {
if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
this.grossSales = grossSales;
public double getGrossSales() {
return grossSales;
}

public void setCommissionRate(double commissionRate) {
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");

this.commissionRate = commissionRate;
}

public double getCommissionRate() {
return commissionRate;
}

public double earnings() {
return getCommissionRate() * getGrossSales();
}

@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", "commission employee", getFirstName(), getLastName(),
"social security number", getSocialSecurityNumber(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
}
}




Fig:9.11
##BasePlusCommissionEmployee extends CommissionEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary;

public BasePlusCommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary) {
super(firstName, lastName, socialSecurityNumber,
grossSales, commissionRate);

if (baseSalary < 0.0) {
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
}

this.baseSalary = baseSalary;
}

public void setBaseSalary(double baseSalary) {

if (baseSalary < 0.0) {
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
}

this.baseSalary = baseSalary;
}

public double getBaseSalary() {
return baseSalary;
}

@Override
public double earnings() {
return getBaseSalary() + super.earnings();
}

@Override
public String toString() {
return String.format("%s %s%n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary());

}
}



Fig:9.13
## LabelDemo

import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class LabelDemo
{
public static void main(String[] args)
{
JLabel northLabel = new JLabel("North");
ImageIcon labelIcon = new ImageIcon("GUItip.gif");
JLabel centerLabel = new JLabel(labelIcon);
JLabel southLabel = new JLabel(labelIcon);
southLabel.setText("South");
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(northLabel, BorderLayout.NORTH);
application.add(centerLabel, BorderLayout.CENTER);
application.add(southLabel, BorderLayout.SOUTH);

application.setSize(300, 300);
application.setVisible(true);

}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.