Part A: Carla created a class called Date. Jameel has been assigned a program to
ID: 3835142 • Letter: P
Question
Part A:
Carla created a class called Date. Jameel has been assigned a program to use the Date class as part of a program to create employee records that include dates. Write a statement that Jameel might need to create a Date object using the class’s default constructor.
Part B:
Carla’s Date class has a function called monthDays that has a month number parameter and returns the number of days in the month passed in. Here is the function definition:
int Date::monthDays(int month)
{
… // actual code not needed
}
Jameel creates a Date object called date1 using an appropriate constructor. Show the statement that Jameel must write to call the monthDays function with the month number 6 and store the results in an appropriate variable.
Part C:
A Circle class has a member function called calcArea that is defined outside of the class declaration. Write the function header for this function assuming it returns a double value.
Explanation / Answer
Hi,
Please see below the classes with comments.
Please comment for any queries/feedbacks.
Thanks.
Date.java
public class Date {
private int date;
private int month;
private int year;
//Part B:
//Carla’s Date class has a function called monthDays that has a month number parameter
//and returns the number of days in the month passed in.
public int monthDays(int month){
int monthDays =0;
if(month ==1 || month ==3 || month ==5 || month ==7 ||month==8 ||month == 10 ||month == 12 ){
monthDays = 31;
}
else if( month == 4 || month ==6 ||month ==9 || month ==11){
monthDays = 30;
}
else if(month ==2){
//Check leap year
int year = this.getYear();
if(year% 4 ==0){
monthDays =29;
}
else {
monthDays =28;
}
}
return monthDays;
}
//Getters and Setters
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
DateDemo.java
public class DateDemo {
public static void main(String [] args){
//Part A:
//Carla created a class called Date.
//Jameel has been assigned a program to use the Date class as part of a program
//to create employee records that include dates.
//Write a statement that Jameel might need to create a Date object
//using the class’s default constructor.
Date date1 = new Date();
//Setting values to object date1
date1.setDate(24);
date1.setMonth(6);
date1.setYear(1990);
//Part B:
//Carla’s Date class has a function called monthDays that has a month number parameter
//and returns the number of days in the month passed in.
//Jameel creates a Date object called date1 using an appropriate constructor. (This is created in part A. Line no: 10)
//Show the statement that Jameel must write to call the monthDays function
//with the month number 6 and store the results in an appropriate variable.
int noOfDaysInMonth = date1.monthDays(6);
System.out.println("No Of Days in Month 6: "+noOfDaysInMonth);
}
}
Sampe output:
No Of Days in Month 6: 30
Circle.java
public class Circle {
public static final double PI =3.14;
private double radius;
//Part C:
//A Circle class has a member function called calcArea
//that is defined outside of the class declaration.
//Write the function header for this function assuming it returns a double value.
public double calcArea(){
double area = PI * radius *radius;
return area;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
CircleDemo.java
public class CircleDemo {
public static void main(String[] args) {
//Creating Circle object
Circle circle1 = new Circle();
//setting the radius
circle1.setRadius(5);
//Calling calcArea onCircle
double area = circle1.calcArea();
System.out.println("Area : "+area);
}
}
Sample output:
Area : 78.5