Question

You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

You are to create a class Appointment.java that will have the following:

5 Instance variables:

  • private String month
  • private int day
  • private int year
  • private int hour
  • private int minute

1 default constructor

public Appointment()

1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables

public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed)

5 setter methods (one for each instance variable) with these limits:

//the setter for the month must determine if the
//month passed is a valid month
//(January - December), if not
//trap the user until they enter a valid month

public void setMonth(String monthPassed)

//the setter for hour must confirm that the hour is
//between 0 and 23 (inclusive) if it is not
//trap the user until they enter a valid hour

public void setHour(int hourPassed)

//the setter for year must confirm that the year
//is >= 0 and <= 2019, if it is not
//trap the user until they enter a valid year

public void setYear(int yearPassed)

//the setter for minute must confirm that the
//minute is between 0 and 59 (inclusive) if it is
//not trap the user until they enter a valid minute

public void setMinute(int minutePassed)

//the setter for day must confirm that the
//day is a valid day for the month and year passed
//if it is not trap the user to enter a valid day for the month
//and year passed to the method (i.e. do not have the user
//reenter the month and year, they are to reenter the day only)
//remember to check if the year is a leap year

public void setDay(int dayPassed, String monthPassed, int yearPassed)

5 getter methods (one for each instance variable) that return the value of the variable

  • public String getMonth()
  • public int getDay()
  • public int getYear()
  • public int getHour()
  • public int getMinute()

1 toString method

public String toString()

1 equals method

public boolean equals(Appointment appointmentPassed)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Appointment{

private String month;

private int day;

private int year;

private int hour;

private int minute;

public Appointment()

{

this.month="";

this.day=0;

this.year=0;

this.hour=0;

this.minute=0;

}

public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed)

{

this.month=monthPassed;

thid.day=dayPassed;

this.year=yearPassed;

this.hour=hourPassed;

this.minute=minutePassed;

}

public void setMonth(String monthPassed)

{

String valid_months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};

int flag=0;

while(flag==0)

{

for(int i=0;i<12;i++)

{

if(valid_months[i].equalsIgnoreCase(monthPassed))

{flag=1;

this.month=monthPassed;

break;}

}

if(flag!=1)

{

System.out.print("Enter a valid month:");

Scanner sc=new Scanner(System.in);

monthPassed=sc.nextLine();

}

}

}


public void setHour(int hourPassed){

int flag=0;

while(flag==0)

{

if(hourPassed>=0 && hourPassed<=23)

{flag=1;this.hour=hourPassed;break;}

else{

System.out.print("Enter a valid hour:");

Scanner sc=new Scanner(System.in);

monthPassed=sc.nextInt();

}

}

}

public void setYear(int yearPassed){

int flag=0;

while(flag==0)

{

if(yearPassed>=0 && year<=2019)

{flag=1;this.year=yearPassed;break;}

else{

System.out.print("Enter a valid year:");

Scanner sc=new Scanner(System.in);

yearPassed=sc.nextInt();

}

}

}

public void setMinute(int minutePassed){

int flag=0;

while(flag==0)

{

if(minutePassed>=0 && minute<=59)

{flag=1;this.minute=minutePassed;break;}

else{

System.out.print("Enter a valid minute:");

Scanner sc=new Scanner(System.in);

minutePassed=sc.nextInt();

}

}

}

public void setDay(int dayPassed,String monthPassed, int yearPassed)

{

int flag=0;

while(flag==0)

{

if(monthPassed.equalsIgnoreCase("January")||monthPassed.equalsIgnoreCase("March")||

monthPassed.equalsIgnoreCase("May")||monthPassed.equalsIgnoreCase("July")||monthPassed.equalsIgnoreCase("August")||monthPassed.equalsIgnoreCase("October")||monthPassed.equalsIgnoreCase("December"))

{

if(dayPassed>=0 && dayPassed<=31)

{this.day=dayPassed;flag=1;break;}

}

else if(monthPassed.equalsIgnoreCase("April")||monthPassed.equalsIgnoreCase("June")||monthPassed.equalsIgnoreCase("September")||monthPassed.equalsIgnoreCase("November")){

if(dayPassed>=0 && dayPassed<=30)

{this.day=dayPassed;flag=1;break;}

}

else if(monthPassed.equalsIgnoreCase("February") && (yearPassed%400==0 )){

if(dayPassed>=0 && dayPassed<=29)

{this.day=dayPassed;flag=1;break;}

}

else if(monthPassed.equalsIgnoreCase("February") && (yearPassed%100==0 ))

{

if(dayPassed>=0 && dayPassed<=28)

{this.day=dayPassed;flag=1;break;}

}

else if(monthPassed.equalsIgnoreCase("February") && (yearPassed%4==0 ))

{

if(dayPassed>=0 && dayPassed<=29)

{this.day=dayPassed;flag=1;break;}

}

else if(monthPassed.equalsIgnoreCase("February") && (yearPassed%4!=0 ))

{

if(dayPassed>=0 && dayPassed<=28)

{this.day=dayPassed;flag=1;break;}

}

else{

System.out.print("Enter a valid day:");

Scanner sc=new Scanner(System.in);

dayPassed=sc.nextInt();

}

}

}

public String getMonth(){return this.month;}

public int getDay(){return this.day;}

public int getHour(){return this.hour;}

public int getMinute(){return this.minute;}

public int getYear(){return this.year;}

public String toString()

{return "Day:"+this.day+" Month:"+this.month+" Year:"+this.year+" hours:"+this.hour+" minutes:"+this.minutes;}

public boolean equals(Appointment appointmentPassed)

{

if(this.month.equalsIgnoreCase(appointmentPassed.getMonth()) && this.day==appointmentPassed.getDay() && this.hour==appointmentPassed.getHour() && this.minute==appointmentPassed.getMinute() && this.year==appointmentPassed.getYear())

return true;

return false;

}

}

Add a comment
Know the answer?
Add Answer to:
You are to create a class Appointment.java that will have the following: 5 Instance variables: private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private...

    We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...

  • In this practical task, you need to implement a class called MyTime, which models a time...

    In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • College of Winston and Charlotte This program will calculate the amount for a semester bill at...

    College of Winston and Charlotte This program will calculate the amount for a semester bill at The College of Winston and Charlotte. Part 1: Create a class Course.java: The class has non-static instance variables: private String department private int courseNumber private int courseCredits private double courseCost The class must have a default constructor which will set values as follows: department = “unknown” courseNumber = 0 courseCost = 0 courseCredits = 0 The class must have a non-default constructor which will...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT