Question

Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...

Why isnt my MyCalender.java working?

class MyCalender.java :


package calenderapp;

import java.util.Scanner;

public class MyCalender {
MyDate myDate;
Day day;
enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
  
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter date below :");
System.out.println("Enter day :");
int day = sc.nextInt();
System.out.println("Enter Month :");
int month = sc.nextInt();
System.out.println("Enter year :");
int year = sc.nextInt();
MyDate md = new MyDate(day, month, year);
if(!md.isDateValid()){
System.out.println("\n Invalid date . Try again. ");
return;
}
MyCalender mc = new MyCalender(md);
System.out.println("\nDay of week :"+ mc.dayOfWeek());
System.out.println("\nWeek of month :" + mc.weekOfMonth());
System.out.println("\nCalender :\n");
mc.printCalender();
  
}
  
MyCalender(MyDate md){
this.myDate = md;
this.day = dayOfWeek();
}
  
Day dayOfWeek(){
int h;
int q = myDate.day;
int m = myDate.month;
if(m == 1) m = 13;
if(m == 2) m = 14;
int k = myDate.year % 100;
if(m==13 || m== 14) k= k-1;
int j = myDate.year / 100;
if(m== 13 || m== 14) j = j-1;
  
double d1 = (double)(13* (m+1))/(double) 5;
double d2 = (double)k / (double)4;
double d3 = (double)j / (double)4;
  
h = q+ (int)d1 + k + (int)d2 + (int)d3 + (5*j);
h = h % 7;
switch(h){
case 0 : return Day.Saturday ;
case 1 : return Day.Sunday;
case 2 : return Day.Monday;
case 3 : return Day.Tuesday;
case 4 : return Day.Wednesday;
case 5 : return Day.Thursday;
case 6 : return Day.Friday;
default : return null;
}
}
  
int weekOfMonth(){
MyDate newDate = new MyDate(1, myDate.month, myDate.year);
Day d = new MyCalender(newDate).dayOfWeek();
int dd = -1;
switch(d){
case Saturday : dd = 6; break;
case Sunday : dd = 0; break;
case Monday : dd = 1; break;
case Tuesday : dd = 2; break;
case Wednesday : dd = 3; break;
case Thursday : dd = 4; break;
case Friday : dd = 5; break;
default : dd = -1; break;
}
int weekOfMonth = 1;
int count = 0;
for(int i =1; i<= dd; i++){
count++;
}
for(int i =0; i<=myDate.day; i++){
if(count % 7 == 0){
weekOfMonth ++;
}
count++;
}
return weekOfMonth;
}
  
void printCalender(){
int daysInMonth = 0;
int year = myDate.year;
int day = myDate.day;
int month = myDate.month;
  
if(year > 1582 && year < 9999){
if(month ==1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day > 0 && day <= 31){
daysInMonth = 31;
}
}
else if(month == 4 || month == 6 || month == 9|| month == 11){
if(day >0 && day <=30){
daysInMonth = 30;
}
}
else {
if(month == 2){
if(year % 400 == 0){
if(day <= 29){
daysInMonth = 29;
}
}
else {
if(day <= 28){
daysInMonth = 28;
}
}
}

}
}
MyDate newDate = new MyDate(1, myDate.month, myDate.year);
Day d = new MyCalender(newDate).dayOfWeek();
int dd = -1;
switch(d){
case Saturday : dd = 6; break;
case Sunday : dd = 0; break;
case Monday : dd = 1; break;
case Tuesday : dd = 2; break;
case Wednesday : dd = 3; break;
case Thursday : dd = 4; break;
case Friday : dd = 5; break;
default : dd = -1; break;
}
int count = 0;
System.out.println("Sun\t Mon\t Tues\t Wed\t Thu\t Fri\t Sat\n");
for(int i =1; i<= dd; i++){
System.out.print("\t");
count++;
}
for(int i =1; i<= daysInMonth; i++){
if(count%7 == 0){
System.out.print("\n");
}
count ++;
System.out.print(i + "\t");
}
System.out.println("\n");
}
}

MyDate.java :


package calenderapp;

public class MyDate {
int day;
int month;
int year;
  
MyDate(int d, int m, int y){
day = d;
month = m;
year = y;
}
int getDay(){
return day;
}
int getMonth(){
return month;
}
int getYear(){
return year;
}
  
boolean isDateValid(){
if(year > 1582 && year < 9999){
if(month ==1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day > 0 && day <= 31){
return true ;
}
}
else if(month == 4 || month == 6 || month == 9|| month == 11){
if(day >0 && day <=30){
return true;
}
}
else {
if(month == 2){
if(year % 400 == 0){
if(day <= 29){
return true;
}
}
else {
if(day <= 28){
return true;
}
}
}

}
}
else {
return false;
}
return false;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the updated code below::

MyCalender.java

package classes15;

import java.util.Scanner;

public class MyCalender {
   MyDate myDate;
   Day day;
   enum Day {
       Monday,
       Tuesday,
       Wednesday,
       Thursday,
       Friday,
       Saturday,
       Sunday
   }

   public static void main(String[] args) {
       Scanner sc= new Scanner(System.in);
       System.out.println("Enter date below :");
       System.out.println("Enter day :");
       int day = sc.nextInt();
       System.out.println("Enter Month :");
       int month = sc.nextInt();
       System.out.println("Enter year :");
       int year = sc.nextInt();
       MyDate md = new MyDate(day, month, year);
       if(!md.isDateValid()){
           System.out.println("\n Invalid date . Try again. ");
           return;
       }
       MyCalender mc = new MyCalender(md);
       System.out.println("\nDay of week :"+ mc.dayOfWeek());
       System.out.println("\nWeek of month :" + mc.weekOfMonth());
       System.out.println("\nCalender :\n");
       mc.printCalender();

   }

   MyCalender(MyDate md){
       this.myDate = md;
       this.day = dayOfWeek();
   }

   Day dayOfWeek(){
       int h;
       int q = myDate.day;
       int m = myDate.month;
       if(m == 1) m = 13;
       if(m == 2) m = 14;
       int k = myDate.year % 100;
       if(m==13 || m== 14) k= k-1;
       int j = myDate.year / 100;
       if(m== 13 || m== 14) j = j-1;

       double d1 = (double)(13* (m+1))/(double) 5;
       double d2 = (double)k / (double)4;
       double d3 = (double)j / (double)4;

       h = q+ (int)d1 + k + (int)d2 + (int)d3 + (5*j);
       h = h % 7;
       switch(h){
       case 0 : return Day.Saturday ;
       case 1 : return Day.Sunday;
       case 2 : return Day.Monday;
       case 3 : return Day.Tuesday;
       case 4 : return Day.Wednesday;
       case 5 : return Day.Thursday;
       case 6 : return Day.Friday;
       default : return null;
       }
   }

   int weekOfMonth(){
       MyDate newDate = new MyDate(1, myDate.month, myDate.year);
       Day d = new MyCalender(newDate).dayOfWeek();
       int dd = -1;
       switch(d){
       case Saturday : dd = 6; break;
       case Sunday : dd = 0; break;
       case Monday : dd = 1; break;
       case Tuesday : dd = 2; break;
       case Wednesday : dd = 3; break;
       case Thursday : dd = 4; break;
       case Friday : dd = 5; break;
       default : dd = -1; break;
       }
       int weekOfMonth = 1;
       int count = 0;
       for(int i =1; i<= dd; i++){
           count++;
       }
       for(int i =0; i<=myDate.day; i++){
           if(count % 7 == 0){
               weekOfMonth ++;
           }
           count++;
       }
       return weekOfMonth;
   }

   void printCalender(){
       int daysInMonth = 0;
       int year = myDate.year;
       int day = myDate.day;
       int month = myDate.month;

       if(year > 1582 && year < 9999){
           if(month ==1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
               if(day > 0 && day <= 31){
                   daysInMonth = 31;
               }
           }
           else if(month == 4 || month == 6 || month == 9|| month == 11){
               if(day >0 && day <=30){
                   daysInMonth = 30;
               }
           }
           else {
               if(month == 2){
                   if(year % 400 == 0){
                       if(day <= 29){
                           daysInMonth = 29;
                       }
                   }
                   else {
                       if(day <= 28){
                           daysInMonth = 28;
                       }
                   }
               }

           }
       }
       MyDate newDate = new MyDate(1, myDate.month, myDate.year);
       Day d = new MyCalender(newDate).dayOfWeek();
       int dd = -1;
       switch(d){
       case Saturday : dd = 6; break;
       case Sunday : dd = 0; break;
       case Monday : dd = 1; break;
       case Tuesday : dd = 2; break;
       case Wednesday : dd = 3; break;
       case Thursday : dd = 4; break;
       case Friday : dd = 5; break;
       default : dd = -1; break;
       }
       int count = 0;
       System.out.println("Sun\t Mon\t Tues\t Wed\t Thu\t Fri\t Sat\n");
       for(int i =1; i<= dd; i++){
           System.out.print("\t");
           count++;
       }
       for(int i =1; i<= daysInMonth; i++){
           if(count%7 == 0){
               System.out.print("\n");
           }
           count ++;
           System.out.print(i + "\t");
       }
       System.out.println("\n");
   }
}

MyDate.java

package classes15;
public class MyDate {
   int day;
   int month;
   int year;

   MyDate(int d, int m, int y){
       day = d;
       month = m;
       year = y;
   }
   int getDay(){
       return day;
   }
   int getMonth(){
       return month;
   }
   int getYear(){
       return year;
   }

   boolean isDateValid(){
       if(year > 1582 && year < 9999){
           if(month ==1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
               if(day > 0 && day <= 31){
                   return true ;
               }
           }
           else if(month == 4 || month == 6 || month == 9|| month == 11){
               if(day >0 && day <=30){
                   return true;
               }
           }
           else {
               if(month == 2){
                   if(year % 400 == 0){
                       if(day <= 29){
                           return true;
                       }
                   }
                   else {
                       if(day <= 28){
                           return true;
                       }
                   }
               }

           }
       }
       else {
           return false;
       }
       return false;
   }
}

output:

Quick AccessJava EE Console X <terminated> MyCalender [Java Application] C:\Program FilesJavare7\bin javaw.exe (May 17, 20 En

Add a comment
Know the answer?
Add Answer to:
Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program...

    Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program that prompts the user to enter a year, month, day of month and it displays the name of the week. Here is my source code. I need it broke down into a flowchart. public static void main(String[] args) {        //all variables are initialized to 0, because local variables require at least an initial value before use        Scanner input = new...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

  • this needs to be in Java: use a comparator class which is passed into the sort...

    this needs to be in Java: use a comparator class which is passed into the sort method that is available on the ArrayList. import java.util.Scanner; public class Assign1{ public static void main(String[] args){ Scanner reader = new Scanner (System.in); MyDate todayDate = new MyDate(); int choice = 0; Library library = new Library(); while (choice != 6){ displayMainMenu(); if (reader.hasNextInt()){ choice = reader.nextInt(); switch(choice){ case 1: library.inputResource(reader, todayDate); break; case 2: System.out.println(library.resourcesOverDue(todayDate)); break; case 3: System.out.println(library.toString()); break; case 4: library.deleteResource(reader,...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding...

    Problem Description: Expand on 2 by implementing Insertion Sort and displaying the dates sorted after adding one to all dates. Run your code using the attached input files outputlong.txtoutputlab3.txt. Please note that your the dates that are sorted will be the ones after 1 day has been added to them. Sample Output: (green is user input) Run #2 using sample file outputlab3.txt Enter name of file to import or the word null to bypass: outputlab3.txt How many assessments in this...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT