Question

In: Computer Science

import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...

import chapter6.date.SimpleDate;
import java.util.Scanner;

public class SimpleDateTestDefault {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
SimpleDate d1 = new SimpleDate();
SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt());
System.out.println(d1);
System.out.println(d2);
System.out.println(d1.before(d2));
System.out.println(d2.before(d1));
}
}

Implement SimpleDate class in chapter6.date package with the following attributes:

  1. day, (int type,  private)
  2. month, (int type,  private)
  3. year (int type,  private)

The class should have the following methods:

  1. a constructor with three parameters: year, month, and day
  2. a constructor with no parameters which initialize the date to 01/01/2000
  3. boolean before(SimpleDate another)
  4. int numOfDays(SimpleDate another), the number of days from this date to another. If another is later than this date, return a positive value, return 0 if equals, and return a negative value if another is before this date.
  5. String toString(): return the String representation in mm/dd/yyyy format.


Solutions

Expert Solution

Code to copy along with screenshots of code and output are provided.
Please refer to screenshots to understand the indentation of code.
If you have any doubts or issues. Feel free to ask in comments
Please give this answer a like, or upvote. This will be very helpful for me.
================================================================

Screenshots of "SimpleData.java":

Screenshots of "SimpleDateTestDefault.java"

Screenshots of Output :

Code to copy("SimpleDate.java"):

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SimpleDate {

   // integer variables
   int day;
   int month;
   int year;
  
   // constructor which takes three integer arguments
   SimpleDate(int day, int month, int year)
   {
      
   // initializing varaible according to arguments
   this.day = day;
   this.month = month;
   this.year = year;
     
   }
  
   // constructor with no parameters
   SimpleDate()
   {
       // initializing with default values
       this.day = 01;
       this.month = 01;
       this.year = 2000;
   }
  
   // method to return number of days from this to another
   int numberOfDays(SimpleDate another)
   {
       int n = 0;
      
       // creating date strings of SimpleDate
       String dateString = Integer.toString(this.month) + "/"+Integer.toString(this.day)+ "/"+Integer.toString(this.year);
   String date2String = Integer.toString(another.month) + "/"+Integer.toString(another.day)+ "/"+Integer.toString(another.year);
  
   // format of date
   String format = "MM/dd/yyyy";
  
   // new SimpleDateFormat object
   SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
  
   try
   {
           // parsing dateString as into Date
       Date d1 = sDateFormat.parse(dateString);
      
       //parsing date2String as into Date
       Date d2 = sDateFormat.parse(date2String);
         
// calculating difference of days using getTime function          
       n = (int) (((d2.getTime()-d1.getTime())/(1000*60*60*24)));
       }
   catch (ParseException e)
   {
       // try catch to catch parsing exception
      
       }
      
       // return number of days
       return n;
   }
  
  
   // method to check if date is before another date
   boolean before(SimpleDate another)
   {
   boolean ret = false;  
      
   // if numberOfDays is positive then another date is after this date
   if(numberOfDays(another) > 0)
   {
   ret = true;  
   }
  
  
   // return
   return ret;
   }
  
   // method to convert this date to string
   public String toString()
   {
       // returning string
       return Integer.toString(this.month)+"/"+Integer.toString(this.day)+"/"+Integer.toString(this.year);
   }

}

Code to copy("SimpleDateTestDefault.java"):

import java.util.Scanner;

public class SimpleDateTestDefault {
  
  
   public static void main(String args[])
  
   {
  
  
   Scanner stdin = new Scanner(System.in);
  
   // creating SimpleDate instance using default values
   SimpleDate d1 = new SimpleDate();
  
  
   System.out.print("Enter day month and year : ");
  
   // creating SimpleDate instance using values entered by user
   SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt());
  
   // converting date to strings
   System.out.println("d1: "+d1.toString());
   System.out.println("d2: "+d2.toString());
  
   // checking before(SimpleDate) function
   System.out.println("Is d1 before d2?: "+d1.before(d2));
   System.out.println("Is d2 before d1?: "+d2.before(d1));
  
   //checking numberOfDays(SimpleDate another) function
   System.out.print("Number of days between(d2 - d1):"+d1.numberOfDays(d2));
  
   }
  
}

==============XXXXXX==============================


Related Solutions

------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new...
import java.util.Scanner; public class Grade { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Reading score from user System.out.println("Enter the student's score:"); double score = scnr.nextDouble(); System.out.print("Grade:"); // Checking if score is less than 60 if(score<60){ System.out.println("F"); } // Checking if score is less than 70 else if(score<70){ System.out.println("D"); } // Checking if score is less than 80 else if(score<80){ System.out.println("C"); } // Checking if score is less than 90 else if(score<90){ System.out.println("B"); } // Checking...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT