Question

public class Date {    private int month;    private int day;    private int year;...

public class Date

{

   private int month;

   private int day;

   private int year;

   public Date( int monthValue, int dayValue, int yearValue )

   {

      month = monthValue;

      day = dayValue;

      year = yearValue;

   }

   public void setMonth( int monthValue )

   {

      month = monthValue;

   }

   public int getMonth()

   {

      return month;

   }

   public void setDay( int dayValue )

   {

      day = dayValue;

   }

   public int getDay()

   {

      return day;

   }

   public void setYear( int yearValue )

   {

      year = yearValue;

   }

   public int getYear()

   {

      return year;

   }

    public void displayDate()

   {

      System.out.printf( "%d/%d/%d", getMonth(), getDay(), getYear() ); } }

Problem: Given the above Date class, write a Test Program to test the class. The program should initialize data fields of two objects, first using Constructor and second using Set methods. Using DisplayDate() Method, print object fields.

Homework Answers

Answer #1
public class Date {
    private int month;
    private int day;
    private int year;

    public Date(int monthValue, int dayValue, int yearValue) {
        month = monthValue;
        day = dayValue;
        year = yearValue;
    }

    public void setMonth(int monthValue) {
        month = monthValue;
    }

    public int getMonth() {
        return month;
    }

    public void setDay(int dayValue) {
        day = dayValue;
    }

    public int getDay() {
        return day;
    }

    public void setYear(int yearValue) {
        year = yearValue;
    }

    public int getYear() {
        return year;
    }

    public void displayDate() {
        System.out.printf("%d/%d/%d", getMonth(), getDay(), getYear());
    }
}

class DateTest {

    public static void main(String[] args) {
        Date date1 = new Date(10, 14, 2020);
        Date date2 = new Date(0, 0, 0);
        date2.setDay(30);
        date2.setMonth(7);
        date2.setYear(2020);

        date1.displayDate();
        System.out.println();
        date2.displayDate();
        System.out.println();
    }
}

Know the answer?
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
Similar Questions
public class Bicycle { // Instance variables (fields) private String ownerName; private int licenseNumber; // Constructor...
public class Bicycle { // Instance variables (fields) private String ownerName; private int licenseNumber; // Constructor public Bicycle( String name, int license ) { ownerName = name;    licenseNumber = license; } // Returns the name of the bicycle's owner public String getOwnerName( ) { return ownerName; } // Assigns the name of the bicycle's owner public void setOwnerName( String name ) { ownerName = name; } // Returns the license number of the bicycle public int getLicenseNumber( ) {...
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions:...
Class 1: Account.java Variables: 1. accountNumber (public) 2. accountName (public) 3. balance (private) In Account.java Functions: 1. Fully parametrized Constructor (to initialize class variables) 2. Getter (getBalance()) and Setter (setBalance()) for the float balance, must be greater than zero. In Account.java Methods: checkBalance( ) to print CURRENT BALANCE. deposit(int added_amount) should add add_amount to balance withdraw(int withdraw_amount) should deduct withdraw_amount balance display( ) will print all the information (account number, name, balance). Class 2: AccountTest.java Create an object account1 and...
Assume the method giveBonus() has been added to the BankAccount class. public class Raise { private...
Assume the method giveBonus() has been added to the BankAccount class. public class Raise { private int annualSalary; public Raise(){ annualSalary = 0; } //end constructor public Raise(int currentSalary){ annualSalary = currentSalary; } //end constructor public void giveRaise(){ annualSalary = annualSalary + 500; } //end giveRaise public void getSalary(){ return annualSalary; } //end giveRaise } What will be the output from the following statements that use this BankAccount class? (assume there is a getBalance() method that returns the balance) Raise...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT