I need help completing this program to not use too much advandce coding, Anyways
ID: 3859747 • Letter: I
Question
I need help completing this program to not use too much advandce coding, Anyways the program is to make a simple calendar like is shown below also are instructions below.
The calendar’s size should be scalable using class constants. You have some creative freedom with creating the calendar. What characters you use for the outline of the calendar and where you place the date in each calendar cell is up to you. An example of what a calendar might look like with a size of 10 after completing this assignment is below:
Your program should produce output similar to this if the size is 10. , as stated above, you have the freedom to choose what characters you use for the calendar (the “|” and “=” in the example above) and the exact location of the number in each cell. Below is an example of a calendar produced with a size of 5:
For now, you may have every month that the calendar displays have 35 days instead of the actual number. We will change that in a later part of the assignment. Upon executing your program, the console should ask what date you would like to look at, as seen in the example above. The use should be able to input a date. The month of that date should be displayed at the top of the calendar. The month and day input by the user should be broken up and displayed at the bottom of the calendar as shown above.
After asking the user for a date and printing the calendar for that date, your program should print a calendar for today’s date followed by the month and day broken up as as seen in the image above.
Ultimately, after execution, your calendar should display a prompt to get a date from the user, a calendar for that month from the user input, the month and day input by the user broken up, a second calendar for the current month, and finally the month and day for the current day broken up.
Implementation Details:
You program must include the following methods:
public static void drawMonth(int month)
This method takes in an integer representing the month and displays the month and a graphical representation of the calendar as seen in the examples above.
public static void drawRow(int row)
This method should be called in your drawCalendar method. It should display one week on the calendar (one row). This method is passed an integer representing which row it is displaying.
public static void displayDate(int month, int day)
This method is passed the month and the day as integer values and displays the date information as seen in the above example underneath the graphical representation of the calendar.
public static int monthFromDate(String date)
This method should extract an integer value for the month and return it when passed a given date as a String. Using the split function may be useful for the operation of this function.
public static int dayFromDate(String date)
This method should extract an integer value for the day and return it when passed a given date as a String. Using the split function may be useful for the operation of this function.
Helpful Information:
For this assignment, you may want to use a few tools that we have not explicitly gone over in class.
In order to get today’s date, you will likely want to make use of a Calendar object that is already implemented by Java. This object stores basic information that we can access. To use this object, you will want to create a new Calendar object (be careful that your class is also not named Calendar or Java will get confused). The code for that would look like Calendar name = Calendar.getInstance(). This gets the current date and stores it in this Calendar object.
To access the information we want, we need to use the Calendar’s get method. For instance, if we wanted the month we would use name.get(Calendar.MONTH), or if we wanted the day we would use name.get(Calendar.DATE). Calendar.MONTH and Calendar.DATE are just ways to refer to the locations where those values are stored.
You also may make use of a number of String functions. The indexOf and substring methods could be of particular use. The indexOf method finds the index of the first occurrence of the given character in the String you call the function on. For example, if we had “Hello” store in a variable x, calling x.indexOf(“l”) would return a value of 2 since the first “l” is located at index 2 in the String “Hello”.
The substring method creates a new string from the starting index given to the ending index given (it does not include the character in the ending index). If not ending index is given, then a substring from the beginning index to the end of the String is created. If we wanted a substring of “Hello” that just captured “ell” and if “Hello” was stored in variable x, we could call x.substring(1, 4). If we wanted just “llo”, we could call x.substring(2).
Finally, you may find it useful to convert a number as a String into an integer value. In order to do this we can call Integer.parseInt(String), where String is the String value of the integer we want.
I strongly encourage you to tackle this assignments in parts and practice iterative design. Work on a few things and then test them to make sure they work. Then, add some more and test those. Make sure that you come up with a plan before you start coding. Writing pseudocode can also be very helpful. I encourage you to speak with your classmates about your development plan for this assignment. I am also available to discuss how you might approach this assignment. Feel free to reach out to me with any questions or concerns.
Style:
It is important that you get used to writing code in good style. What is demonstrated in examples in class is considered good style. Additionally, you should look at the style guide located on Cavas. Code written not in good style will lose points.
Extra Credit Opportunities (Optional):
You can choose to do as many or as few of these as you would like. Each one completed successfully and in good style will earn some extra credit points towards this assignment.
1. As stated above in the specification, you are allowed to let your calendar run to 35 days. For an added challenge, you can limit the number of days to 31. Completing this task successfully and in good style will result in extra credit.
what date would you like to look at? 9/20 8 10 12 13 14 l 15 16 17 18 19 2e 21 23 24 25 26 | 27 28 29 30 31 32 34 l 35 Month: 9 Day: 2eExplanation / Answer
Given below is the code to draw the calendar. I could have used format string with System.out.printf(), to do padding of spaces, but was not sure if you would be comfortable. Using only print() and println() in the program. Please rate if the answer helped. Thank you.
import java.util.Calendar;
import java.util.Scanner;
public class DrawCalendar {
private static int SIZE = 10; //sets the width of each cell in the calendar
private static char HORIZONTAL = '=';
private static char VERTICAL = '|';
public static void drawMonth(int month){
int center = SIZE * 3; //calculate the near center where to display month number
String space = " ";
//display blanks upto center
System.out.println();
for(int i = 0; i< center; i++)
System.out.print(space);
//now display month number
System.out.print(month + " ");
//display each of the 5 rows
for(int row = 1; row <= 5; row++)
{
drawRow(row);
}
//draw the final horizontal line with HORIZONTAL character
int width = SIZE * 7 ;
String str = "";
for(int i = 0; i <= width; i++)
str += HORIZONTAL;
System.out.println(str);
}
public static void drawRow(int row){
String str = "";
int width = SIZE * 7 ; //total width of the row
//1st line: make string full of horizontal characters for total width and display it
for(int i = 0; i <= width; i++)
str += HORIZONTAL;
System.out.println(str);
//2nd line consist of vertical character followded by day number followed by padding spaces to match the size of cell
//this pattern of <day> <padding> <vertical_char> will repeat for each cell
int day;
String space = " ";
System.out.print("|"); //start with vertical character
for(int cell = 1; cell <= 7; cell++){
//make the cell pattern descibed above
str = "";
day = (row - 1) * 7 + cell; //calculate the day that will displayed in cell
//extra credit..... don't append day ,if more than 31
if(day <= 31)
str += day;
//pad it with extra spaces to match SIZE
while(str.length() < SIZE - 1)
str += space;
str += VERTICAL; //append a vertical character
System.out.print(str); //display the string formed for the cell
}
//now remaining lines will be similar to above but the day number is not there... only spaces padded to match cell SIZE
// the pattern for each cell is <padding_spaces><vertical_char>
int height = SIZE/2;
for(int h = 2; h <= height; h++){
System.out.print(" |");
for(int cell = 1; cell <= 7; cell++){
str = "";
while(str.length() < SIZE - 1)
str += space;
str += VERTICAL;
System.out.print(str);
}
}
System.out.print(" ");
}
public static void displayDate(int month, int day){
System.out.println("Month: " + month);
System.out.println("Day: " + day);
}
public static int monthFromDate(String date){
String tokens[] = date.split("/"); //split the date into 2 based on / delimiter
//convert 1st token and return as month
return Integer.parseInt(tokens[0]);
}
public static int dayFromDate(String date){
String tokens[] = date.split("/"); //split the date into 2 based on / delimiter
//convert 2nd token and return as day
return Integer.parseInt(tokens[1]);
}
public static void drawCalendar(int month, int day){
drawMonth(month);
displayDate(month, day);
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
//get user input date
System.out.println("Enter a date (m/d): ");
String date = keybd.next();
int day = dayFromDate(date);
int month = monthFromDate(date);
drawCalendar(month, day);
//display calendar for today's date
Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1; //add 1 since month start from 0;
System.out.println(" Displaying calendar for today's date " + month + "/" + day);
drawCalendar(month, day);
}
}
output
Enter a date (m/d):
9/20
9
=======================================================================
|1 |2 |3 |4 |5 |6 |7 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|8 |9 |10 |11 |12 |13 |14 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|15 |16 |17 |18 |19 |20 |21 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|22 |23 |24 |25 |26 |27 |28 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|29 |30 |31 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
Month: 9
Day: 20
Displaying calendar for today's date 7/24
7
=======================================================================
|1 |2 |3 |4 |5 |6 |7 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|8 |9 |10 |11 |12 |13 |14 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|15 |16 |17 |18 |19 |20 |21 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|22 |23 |24 |25 |26 |27 |28 |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
|29 |30 |31 | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
=======================================================================
Month: 7
Day: 24
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.