Resetting array index when reaching the last index - java

I'm trying to build a code in which the 12 months of the calendar are in an array, the code then asks for a month, and then it outputs the 10 months ahead of the inputted month.
e.g.
input: January
output: February, March,...November
I'm having trouble reverting the index back to the start once the index exceeds 12, for example, if I input August, it's supposed to output September to June, but instead it stops at December and says out of bounds. Thank you
String months[];
String choice;
months = new String[13];
months[0] = null ;
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
System.out.print("Enter Month : ");
choice = a.nextLine();
if (choice.equals("August")) {
for(int i=8; i<i+10; i++) {
String result= months[i];
System.out.println(result);
}
}

Use the modulus when choosing the month to print:
int start = 8;
if ("August".equals(choice)) {
for(int i=start; i < start+10; i++) {
String result= months[i % 12];
System.out.println(result);
}
}
This assumes you have defined your months array as:
String[] months = new String[12];
months[0] = "January";
months[1] = "February";
// ...
months[11] = "December";
The idea here is to wrap around the index used to select a month from the array. The dummy variable i, upon hitting the value 12, will wrap around to zero again.
Side note: It is always better to compare a string literal to a variable by placing the literal on the LHS of the comparison. The version I used is immune to a null pointer exception; the version you used is not.

Use modulus to reset the array index. The solution given above is correct. But if you want to continue with your own defined months array, where months[0] = null ;, just start the for loop from the next index of the input month.
if ("August".equals(choice)) {
int start = 9;
for(int i=start; i < start+11; i++) {
if(i!=13){
String result= months[i % 13];
System.out.println(result);
}
}
}

Try this,
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String months[];
String choice;
months = new String[13];
months[0] = null;
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
System.out.print("Enter Month : ");
choice = scanner.nextLine();
int startIndex = 0;
for (int i = 0; i < months.length; i++) {
if (choice.equalsIgnoreCase(months[i])) {
startIndex = i + 1;
}
}
for (int j = startIndex; j < months.length; j++) {
String result = months[j];
System.out.println(result);
}
}

Related

How to add 14 based numbers in instance method?

So I'm having trouble with creating an instance method to add two 14-based number and I was wondering if anyone could help? I'm a bit new to java and still sort of confused on the whole thing. So far I have the code to convert the 14-based numbers to base 10 then I need to add them and convert them back to base-14. I want to put them all in once instance class, but I feel like it's too much to put into one instance class.
This is the kind of input I was for the client code to be like this:
PokerNum sum = num1.add(num2);
import java.util.Scanner;
public class PokerNum{
String alienNum;
int num1, num2;
public PokerNum(String alienNum) throws IllegalArgumentException{
this.alienNum = alienNum;
String toUpper = alienNum.toUpperCase();
for (int i = 0; i < toUpper.length(); i++){
char c = toUpper.charAt(i);
if (!(Character.isDigit(c) || c == 'A' || c == 'J' || c == 'Q' || c == 'K')){
throw new IllegalArgumentException("invalid input");
}
}
}
// initialized at zero
public PokerNum() {
this.num1=0;
this.num2=0;
}
#Override public String toString(){
return PokerNum()+ " ";
}
//add pokernums
public PokerNum add(PokerNum another){
PokerNum num = new PokerNum();
this.num1 = another.num1;
this.num2 = another.num2;
public PokerNum convert(PokerNum pn){
char[] firstNum = num1.toCharArray();
int amountValue = 0;
int length = characters.length;
for (int index = 0; index < length; index++){
int symbolValue;
switch (characters[index]) {
case 'A':
symbolValue = 10;
break;
case 'J':
symbolValue = 11;
break;
case 'Q':
symbolValue = 12;
break;
case 'K':
symbolValue= 13;
break;
default:
symbolValue = characters[index] - 48;
}
amountValue += symbolValue*Math.pow(14, length - index - 1);
}
StringBuilder result1 = new StringBuilder();
while(amountValue > 0){
int digit = amountValue%14;
switch (digit) {
case 10:
result.insert(0, 'A');
break;
case 11:
result.insert(0, 'J');
break;
case 12:
result.insert(0, 'Q');
break;
case 13:
result.insert(0, 'K');
break;
default:
result.insert(0, digit);
}
amountValue-=digit;
amountValue/=14;
String firstValue = result1.toString();
}
}
char[] secNum = num2.toCharArray();
int amountValue2= 0;
int length = secNum.length;
for (int index = 0; index < length; index++){
int symbolValue;
switch (characters[index]) {
case 'A':
symbolValue = 10;
break;
case 'J':
symbolValue = 11;
break;
case 'Q':
symbolValue = 12;
break;
case 'K':
symbolValue= 13;
break;
default:
symbolValue = characters[index] - 48;
}
amountValue2+= symbolValue*Math.pow(14, length - index - 1);
}
StringBuilder result = new StringBuilder();
while(amountValue2> 0){
int digit = amountValue%14;
switch (digit) {
case 10:
result.insert(0, 'A');
break;
case 11:
result.insert(0, 'J');
break;
case 12:
result.insert(0, 'Q');
break;
case 13:
result.insert(0, 'K');
break;
default:
result.insert(0, digit);
}
amountValue2-=digit;
amountValue2/=14;
PokerNum secondValue = result.toString();
}
return firstValue + secondValue;
/*PokerNum sum = num1 +
PokerNum another.num2 =
return sum;*/
}
Thanks to anyone who helps in advance :)
I believe you can keep it in one instance. One suggestion is to use the conversion using the following functions:
Integer.parseInt(String s, int radix)
Integer.toString(int i, int radix)
The javadoc says that the string produced will use:
0123456789abcdefghijklmnopqrstuvwxyz
So, if we choose radix = 14, it will have 0123456789abcd.
The logic idea is to keep the actual number as int, and to convert at creation and at printout from and to String. We can just keep member variable num1 and remove the member variable alienNum and num2.
Constructor
Your constructor with String argument seems to do a good error checking. What we need here is just to convert from string to integer and store it in the member variable. First we need to convert all valid string from AJQK to ABCD. The example here is inefficient, but gets the idea across:
//alienNum = alienNum.replace('A', 'A'); // no need
alienNum = alienNum.replace('J', 'B');
alienNum = alienNum.replace('Q', 'C');
alienNum = alienNum.replace('K', 'D');
Then we can call the parsing method:
num1 = Integer.parseInt(alienNum , 14);
Your empty arg constructor is already fine by initializing the value of num1 to 0.
Adding
The method inside addition is not right because it is setting the current value to the addition. There are three objects working here: num, this, and another. You want to add this to another into num and return num.
num.num1 = this.num1 + another.num1;
Output
I'm assuming the output is going to be from toString(). In this case, you want to convert from integer to string, then convert it to the right character
String out = Integer.toString(num1, 14).toUpper();
//alienNum = alienNum.replace('A', 'A'); // no need
alienNum = alienNum.replace('B', 'J');
alienNum = alienNum.replace('C', 'Q');
alienNum = alienNum.replace('D', 'k');
You probably won't need a convert method now.

Java - Displaying the temperature statistics of the week using arrays

I am currently working on a problem set for an assignment at school, and I'm really close to finishing however I'm getting a few compilation errors.
The problem set includes displaying the weeks avg. temp, highest temp., lowest temp., and the days of the week that are hottest and coldest.
Currently what I'm trying to do is display the days of the week that are hottest, and if I work that out I can easily find the coldest days of the week.
I'm getting a few compilation errors when I try to compile the code which includes
incompatible types: int[] cannot be converted to int
error: cannot find symbol
It would be great if I could get some guidance on what to do, I'm currently at lost right now.
http://ideone.com/rOqV2Z
public class test1
{
// Main method
public static void main(String[] args)
{
// Create a new scanner
Scanner input = new Scanner(System.in);
// Set array list
int[] tempList = new int[7];
// Prompt user for input and store input
System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
for(int i = 0; i < tempList.length; i++)
tempList[i] = input.nextInt();
// Averages temperature - ####### ASK WHY IT THERE ARE SO MANY DECIMALS ON THE SIDE WHEN AVERAGE ALL 1's
double avgTemp = avgTemp(tempList);
System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);
// Display hottest temperature
int maxTemp = maxTemp(tempList);
System.out.println("The highest temperature of the week is: " + maxTemp + " degree");
// Display coldest temperature
int minTemp = minTemp(tempList);
System.out.println("The coldest temperature of the week is: " + minTemp + " degree");
int[] maxTempList = searchTemp(tempList, maxTemp);
for(int i = 0; i < maxTempList.length; i++){
System.out.print("The hottest days of the week are: " +maxTempList[i]);
System.out.print(weekDay(tempList,maxTemp));
}
}
// Average the temperature
public static double avgTemp(int[] array)
{
int tempTotal = array[0];
// Total temperature values
for(int i = 0; i < array.length; i++)
tempTotal = array[i]+tempTotal;
// Return temperature average.
return ((double)tempTotal/array.length);
}
// Get hottest temperature
public static int maxTemp(int[] array)
{
int max = array[0];
// Check and replace max temp
for(int i = 1; i < array.length; i++){
if(max < array[i])
max = array[i];
}
return max;
}
// Get coldest temperature
public static int minTemp(int[] array)
{
int min = array[0];
for(int i = 1; i < array.length; i++){
if(min > array[i])
min = array[i];
}
return min;
}
// Return days
public static String weekDay(int i, int[] array)
{
int[] displayWeekDay = searchTemp(array, i);
for(i = 0; i < displayWeekDay.length; i++){
String weekDay = "";
switch(i)
{
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesdays";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
}
}
return weekDay;
}
// Finds the index of the hottest/coldest days
public static int[] searchTemp(int[] temp, int key)
{
int count = 0;
for(int i = 0; i < temp.length; i++){
if(temp[i] == key)
count++;
}
int[] index = new int[count];
for(int j = 0; j < index.length; j++){
for(int i = 0; i < temp.length; i++){
if(temp[i] == key){
if(j > 0 && index[j - 1] == i)
continue;
else{
index[j] = i;
break;
}
}
}
}
return index;
}
}
I went and checked the code using the website you linked.
Firstly, you should learn to use the debugger, as it will usually tell you what the error is and where to find it.
Main.java:42: error: incompatible types: int[] cannot be converted to int
System.out.print(weekDay(tempList,maxTemp));
^
Here it points to an error on the data type of tempList. It's saying that an int array cannot be converted to an int. If you look at the weekDay() function you'll see that the first argument is asking for an int, but you are passing an int array. It won't work.
public static String weekDay(int i, int[] array)
EDIT: If you want to pass a specific value into the function from the array just use
System.out.print(weekDay(tempList[IntegerPosition],maxTemp));
^
Main.java:104: error: cannot find symbol
return weekDay;
^
This simply means it can't find the variable in the current scope. There's a lot to learn about this, but I'll just get to the point.
// Return days
public static String weekDay(int i, int[] array)
{
int[] displayWeekDay = searchTemp(array, i);
String weekDay = "";
for(i = 0; i < displayWeekDay.length; i++){
//String weekDay = ""; Declare weekDay outside of the loop
switch(i)
{
//Assign a value to weekDay, simply returning won't do it
case 0: weekDay = "Sunday"; break;
case 1: weekDay = "Monday"; break;
case 2: weekDay = "Tuesday"; break;
case 3: weekDay = "Wednesdays"; break;
case 4: weekDay = "Thursday"; break;
case 5: weekDay = "Friday"; break;
case 6: weekDay = "Saturday"; break;
}
}
return weekDay;
}
EDIT 2: As per the discussion, this is what I would do in order to be able to print multiple days that had the highest temperature
//Call the function directly without putting a print statement around it
weekDay(maxTemp,tempList));
//...
// Return days
public static void weekDay(int i, int[] array) //Change the return type to void
{
int[] displayWeekDay = searchTemp(array, i);
for(i = 0; i < displayWeekDay.length; i++){
switch(displayWeekDay[i])
{
//Print each one
case 0: System.out.println("Sunday"); break;
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
}
}
}
Addressing each issue on it's own:
"incompatible types: int[] cannot be converted to int" issue
This is being caused by the line System.out.print(weekDay(tempList,maxTemp));. The method signature for the weekDay method is public static String weekDay(int i, int[] array) however the method is being called with arguments in the wrong order - tempList is of type int[] and maxTemp is of type int. Reversing the arguments in either the method call or the method signature will resolve the error.
"error: cannot find symbol"
This is an issue related to variable scope. When a variable is declared (e.g. int i; or String name = "John";), that variable can only be used within the scope that it is declared in. In the weekDay method the weekDay variable is declared inside the for loop (i.e. inside of the braces associated with the for loop). As such the weekDay variable only has the scope of the for loop, and cannot be referenced outside of that scope. Moving the declaration of weekDay outside of the for loop will fix the issue. See here for more information on variable scope rules.
Like I said, think about the API first, input later. This implementation assumes JDK 8 and lambdas:
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
* Created by Michael
* Creation date 3/19/2016.
* #link https://stackoverflow.com/questions/36107614/java-displaying-the-temperature-statistics-of-the-week-using-arrays
*/
public class TemperatureHistory {
private Map<Date, Double> temperatureHistory = new TreeMap<>();
public void addDataPoint(Date date, Double temperature) {
if (date != null && temperature != null) {
this.temperatureHistory.put(date, temperature);
}
}
public Double getAverageTemperature() {
double averageTemperature = 0.0;
if (this.temperatureHistory.size() > 0) {
averageTemperature = this.temperatureHistory.values()
.stream()
.collect(Collectors.averagingDouble(value -> value));
}
return averageTemperature;
}
public Double getMaxTemperature() {
return this.temperatureHistory.entrySet()
.stream()
.max((e1, e2) -> e1.getValue().compareTo(e2.getValue()))
.get()
.getValue();
}
public Double getMinTemperature() {
return this.temperatureHistory.entrySet()
.stream()
.min((e1, e2) -> e1.getValue().compareTo(e2.getValue()))
.get()
.getValue();
}
public Date getFirstDateForTemperature(Double temperature) {
return this.temperatureHistory.entrySet()
.stream()
.filter(e -> e.getValue().equals(temperature))
.map(Map.Entry::getKey)
.findFirst()
.orElse(null);
}
public Date getDateMinTemperature() {
return this.getFirstDateForTemperature(this.getMinTemperature());
}
public Date getDateMaxTemperature() {
return this.getFirstDateForTemperature(this.getMaxTemperature());
}
}
In the last statement of you main method you do System.out.print(weekDay(tempList,maxTemp));
weekDay takes an int as the first argument, but tempList is of type int[]. You should swap the order of tempList and maxTemp either in your method call or definition.
public static String weekDay(int i, int[] array)
In this method, you're suppose to return a String type value
edit: change the string variable to other name. you can't have it the same as the method's name

I need another pair of eye to tell me where I am being dumb again (about calculations)

I am actually trying to do something like calculating the day of the week by knowing the date e.g.February 20, 1950 which is a Monday. Of course there are lots other concerns and I do have the formula already.
The thing is somehow when I added an if statement that I am suppose to get let's say 3 and I got 2, if I should get 0 I get -1. Can someone please give me an eye to see what's wrong?
These are the month code variables which will be the output I need and this is the part where the output keep on getting the wrong output than the expected.
/* Month Code */
private static int JAN_CODE = 1;
private static int FEB_CODE = 4;
private static int MAR_CODE = 4;
private static int APR_CODE = 0;
private static int MAY_CODE = 2;
private static int JUN_CODE = 5;
private static int JUL_CODE = 0;
private static int AUG_CODE = 3;
private static int SEP_CODE = 6;
private static int OCT_CODE = 1;
private static int NOV_CODE = 4;
private static int DEC_CODE = 6;
leap year calculation and returns true or false
private boolean isLeapYear(){
if((year % 100 == 0) && (year % 400 != 0))
{
return false;
}else if(year % 4 == 0)
{
return true;
}else{
return false;
}
}
this is the part where to return the monthCode depending on what the month is in integer
public int getMonthCode(){
// variable to store the code
int monthCode;
// January and February dates in leap years: subtract 1
boolean i = isLeapYear();
if (i == true){
JAN_CODE -= 1;
FEB_CODE -= 1;
}
// Check which month uses which month code
switch (month){
case JANUARY_INT: monthCode = JAN_CODE;
break;
case FEBURARY_INT: monthCode = FEB_CODE;
break;
case MARCH_INT: monthCode = MAR_CODE;
break;
case APRIL_INT: monthCode = APR_CODE;
break;
case MAY_INT: monthCode = MAY_CODE;
break;
case JUNE_INT: monthCode = JUN_CODE;
break;
case JULY_INT: monthCode = JUL_CODE;
break;
case AUGUST_INT: monthCode = AUG_CODE;
break;
case SEPTEMBER_INT: monthCode = SEP_CODE;
break;
case OCTOBER_INT: monthCode = OCT_CODE;
break;
case NOVEMBER_INT: monthCode = NOV_CODE;
break;
case DECEMBER_INT: monthCode = DEC_CODE;
break;
default: monthCode = 0;
}
return monthCode;
}
a simple code to see what's returned
public void getCode(){
System.out.println("Code: "+getMonthCode());
}
I tried using JAN_CODE = JAN_CODE - 1; which still gets the wrong output.
So let's say if January is entered, the JAN_CODE I am getting would be 1 if not a leap year but if it is a leap year it should be 0 but after I put in the if(i == true) statement to see if it is leap year and January is entered, the monthCode I get is -1, if February I get 2 but I am suppose to get 0 for Jan and 3 for Feb isn't it?
If I take out the if statement then what is returned is 1 for Jan and 4 for Feb.
Can someone give me a hand?
I would start by modifying how your function manipulates the data, passing parameters rather than using static content. This should give you an idea of how it can be solved:
private boolean isLeapYear(inYear){
if((inYear % 100 == 0) && (inYear % 400 != 0))
{
return false;
}else if(inYear % 4 == 0)
{
return true;
}else{
return false;
}
}
private int getNewMonthCode(inYear, inMonthCode) {
if (isLeapYear(inYear)) {
return inMonthCode-1;
}
return inMonthCode;
}
public int getMonthCode(inYear, inMonth) {
// variable to store the code
int monthCode;
// Check which month uses which month code
switch (inMonth){
case JANUARY_INT: monthCode = getNewMonthCode(inYear, JAN_CODE);
break;
case FEBURARY_INT: monthCode = getNewMonthCode(inYear, FEB_CODE);
break;
case MARCH_INT: monthCode = MAR_CODE;
break;
case APRIL_INT: monthCode = APR_CODE;
break;
case MAY_INT: monthCode = MAY_CODE;
break;
case JUNE_INT: monthCode = JUN_CODE;
break;
case JULY_INT: monthCode = JUL_CODE;
break;
case AUGUST_INT: monthCode = AUG_CODE;
break;
case SEPTEMBER_INT: monthCode = SEP_CODE;
break;
case OCTOBER_INT: monthCode = OCT_CODE;
break;
case NOVEMBER_INT: monthCode = NOV_CODE;
break;
case DECEMBER_INT: monthCode = DEC_CODE;
break;
default: monthCode = 0;
}
return monthCode;
}
Called by System.out.println("Code: " + getMonthCode(1954,1)); //January 1954
You can still use your global/static variables: getMonthCode(year,month)

Java Calendar Program

I have a project due soon. We have to write a program that asks for a year and prints out a calendar. We can only use one while, one for, and one switch loop. (and no arrays! ugh) Im having trouble figuring out how to print out the days of each month, starting with the first day of the first week, as most months will not start on Sunday.
import java.util.*;
import java.util.Calendar;
class Lab2 {
public static void main(String[] args) {
Scanner user = new Scanner(System.in);
System.out.print("What year do you want to view? ");
int year = user.nextInt();
System.out.printf("%12d\n", year);
System.out.println();
boolean leap = isLeap(year);
int firstDay = JulianDate(year);
monthLoop(year, firstDay, leap);
}
public static boolean isLeap(int year) {
boolean verdict = false;
if (year % 100 == 0 && year % 400 == 0) {
verdict = true;
}
if(year % 100 != 0 && year % 4 == 0) {
verdict = true;
}
return verdict;
}
public static int JulianDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_YEAR, 1);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return dayOfWeek;
}
public static void monthLoop(int year, int firstDay, boolean leap) {
for(int i=1; i <= 12; i++) {
switch (i) {
case 1: System.out.printf("%13s\n", "January");
break;
case 2: System.out.printf("%13s\n", "February");
break;
case 3: System.out.printf("%12s\n", "March");
break;
case 4: System.out.printf("%12s\n", "April");
break;
case 5: System.out.printf("%11s\n", "May");
break;
case 6: System.out.printf("%11s\n", "June");
break;
case 7: System.out.printf("%11s\n", "July");
break;
case 8: System.out.printf("%13s\n", "August");
break;
case 9: System.out.printf("%14s\n", "September");
break;
case 10: System.out.printf("%13s\n", "October");
break;
case 11: System.out.printf("%14s\n", "November");
break;
case 12: System.out.printf("%14s\n", "December");
break;
}
System.out.println("S M Tu W Th F S");
}
}
}
You would get the day of the week, as in this post, of the first day of the month and then start the counter from there.
How to get the day of the week in Java
For instance, if the day of the week was 5, you would put 4 "blanks" before the first date. The trick is that when you are doing your mod, to determine if there should be a new line, it would be
(dayofMonth + firstDayOfWeekOfMonth) % 7

Printing the rest of the calendar month after the first week

i am making a program that reads in the month 1-12 and the start day, 1-7 for sunday, monday, etc. it prints the first week fine but im having problems printing the rest of the days after the first week. it wont format the output to have a space between the rest of the days, only the first day after the first week.
side note: Keyboard is a class i have included in my project
heres my code:
import java.io.*;
public class CalendarMonth
{
static final int WEEK = 7;
public static void main(String[] args) throws IOException
{
String proceed;
char repeatCheck;
int days;
String leapYear;
char leapYearCheck;
int startDay;
do
{
days = getDays();
System.out.println(days);
System.out.println("Please enter a number 1-7 that the month starts on: (1 for sunday, 2 for monday, etc)");
startDay = Keyboard.readInt();
while(startDay < 1 || startDay > 7)
{
System.out.println("You did not enter a number 1-7, Try again: ");
startDay = Keyboard.readInt();
}
System.out.println(" s m t w th f sa");
printMonth(days,startDay);
System.out.println("\nWould you like to print another month?");
proceed = Keyboard.readString();
repeatCheck = proceed.charAt(0);
}while(repeatCheck == 'y');
}
public static int getDays() throws IOException
{
int month;
String leapYear;
int startDay;
int days = 0;
char leapYearCheck;
System.out.println("Please input a number 1-12 to print the corresponding month: ");
month = Keyboard.readInt();
while (month < 1 || month > 12)
{
System.out.println("You did not put a number 1-12, Try again: ");
month = Keyboard.readInt();
}
switch(month)
{
case 1: days = 31;
break;
case 2:
System.out.println("is it a leap year? ");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);
while(leapYearCheck != 'y' && leapYearCheck != 'n')
{
System.out.println("you did not enter a yes or no answer, is it a leap year?");
leapYear = Keyboard.readString();
leapYearCheck = leapYear.charAt(0);
}
if (leapYearCheck == 'y')
{
days= 29;
}
else
{
days = 28;
}
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
return days;
}
public static void printMonth(int days, int startDay)
{
int count;
count = startDay;
for (int counter = 1; counter <= 7; counter++)
{
if (counter < startDay)
{
System.out.print(" ");
count = count-1;
}
else
{
System.out.printf("%2d", count);
count++;
}
}
//int restOfTheMonth = (WEEK - startDay);
System.out.println();
for(int restOfTheMonth = count; restOfTheMonth <= days; restOfTheMonth++)
{
if (restOfTheMonth%WEEK==0)
{
System.out.println();
}
System.out.printf("%2d", restOfTheMonth);
}
}
}

Categories

Resources