STEP 1: Cylinder Create a class called Cylinder.java that contains two double-pr
ID: 3542463 • Letter: S
Question
STEP 1: Cylinder Create a class called Cylinder.java that contains two double-precision instance variables named radius and height. The class should include a constructor that initializes the radius and height variables. Also, you need a class method named volume() that returns the volume of a Cylinder object. The volume of a cylinder is given by its radius squared times its height times Pi (radius * radius * height * Math.PI). You can either use the value 3.1416 for Pi or use the Java provided value named Math.PI. Write a class called CylinderTest.java and declare an array of three Cylinder objects to call the methods you declared in the Cylinder class. Make sure that all class methods are called from main(). Have main() display the value returned by volume() and verify the returned value by hand calculations (paper/pencil). Prompt the user to enter the values for the radius and height of each Cylinder object in the array. STEP 2: Date Create a program called Date.java to perform error-checking on the initial values, for instance: fields month, day, and year. Also, provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state. Write a program called DateTest.java that prompts the user to enter the month, day, and year as numeric values. This program then creates a Date object using the Date class you just created and tests the nextDay() method. This can be done in a loop of 40 iterations: the Date object calls the nextDay() method and prints the date during each iteration of the loop. This loop is to illustrate that the nextDay() method works correctly. Test the following cases: Incrementing into the next month, for example, use date: 02/28/2011 Incrementing into the next year, for example, use date: 11/27/2011 Incrementing into the next month in a leap year, for example, use date: 02/28/2012 Sample Program Output: Checking increment Date object constructor for date 11/27/2011 Incremented Date:11/28/2011 Incremented Date:11/29/2011 Incremented Date:11/30/2011 Day 31 invalid. Set to day 1. Incremented Date:12/1/2011 Incremented Date:12/2/2011 ... Incremented Date:12/30/2011 Incremented Date:12/31/2011 Day 32 invalid. Set to day 1. Incremented Date:1/1/2012 Incremented Date:1/2/2012 Incremented Date:1/3/2012 Incremented Date:1/4/2012 Incremented Date:1/5/2012 Incremented Date:1/6/2012Explanation / Answer
Hi, Here is the part 2....
public class DateTest
{
public static void main(String [] args)
{
Date d = new Date(2,8,2011);
for(int i=0;i<40;i++)
{
d = d.nextDay();
System.out.println(d.month+"/"+ d.day+"/"+d.year);
}
}
}
public class Date
{
public int month;
public int day;
public int year;
public Date(int m,int d, int y)
{
month = m;
day = d;
year=y;
}
public Date nextDay()
{
int leap=0;
//check for leap year
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
leap=1;
// check month ends
if(month ==4 || month==6 || month==9 || month==11)
{
if(day==30 )
{
month = month+1;
day = 1;
}
else
day = day+1;
}
else if(month == 1 ||month == 3 ||month == 5 ||month == 7 ||month == 8 ||month == 10 )
{
if(day==31 )
{
month = month+1;
day =1;
}
else
day = day+1;
}
else if(month==2)
{
if(leap==1)
{
if(day==28)
{
month =2;
day=29;
}
else if(day==29)
{
month =3;
day=1;
}
else
day = day+1;
}
else if(leap ==0)
{
if( day ==28) {
month=3;
day = 1;
}
else
{
day = day+1;
}
}
}
else if(month==12 )
{
if(day==31)
{year = year+1;
month=1;
day= 1; }
else
{
day = day +1;
}
}
return new Date(month,day,year);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.