ACEINFERNO

untittled

Jul 16th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. Overloading Method Names
  2.  import java.util.Scanner;
  3.  public class DateSixthTry    
  4.  {    
  5.  private String month;  
  6.  private int day;    
  7.  private int year; //a four digit number.    
  8.  public void setDate(int monthInt, int day, int year)    
  9.  {    
  10.  if (dateOK(monthInt, day, year))    
  11.  {    
  12.  this.month = monthString(monthInt);    
  13.  this.day = day;    
  14.  this.year = year;  
  15.  }    
  16.  else    
  17.  {    
  18.  System.out.println("Fatal Error");    
  19.  System.exit(0);    
  20.  }    
  21.  }enter code here    
  22.  public void setDate(String monthString, int day, int year)    
  23.  {    
  24.  if (dateOK(monthString, day, year))    
  25.  {    
  26.  this.month = monthString;    
  27.  this.day = day;    
  28.  this.year = year;  
  29.  }    
  30.  else    
  31.  {    
  32.  System.out.println("Fatal Error");  
  33.  System.exit(0);    
  34.  }  
  35.  }    
  36.  public void setDate(int year)    
  37.  {    
  38.  setDate(1, 1, year);    
  39.  }  
  40.  private boolean dateOK(int monthInt, int dayInt, int yearInt)  
  41.  {    
  42.  return ( (monthInt >= 1) && (monthInt <= 12) &&    
  43.  (dayInt >= 1) && (dayInt <= 31) &&    
  44.  (yearInt >= 1000) && (yearInt <= 9999) );    
  45.  }    
  46.  private boolean dateOK(String monthString, int dayInt, int yearInt)  
  47.  {  
  48.  return ( monthOK(monthString) &&    
  49.  (dayInt >= 1) && (dayInt <= 31) &&    
  50.  (yearInt >= 1000) && (yearInt <= 9999) );  
  51.  }    
  52.  private boolean monthOK(String month)  
  53.  {  
  54.  return (month.equals("January") || month.equals("February") ||  
  55.  month.equals("March") || month.equals("April") ||    
  56.  month.equals("May") || month.equals("June") ||
  57.  month.equals("July") || month.equals("August") ||
  58.  month.equals("September") || month.equals("October") ||  
  59.  month.equals("November") || month.equals("December") );
  60.  }  
  61.  public void readInput()    
  62.  {
  63.  boolean tryAgain = true;  
  64.  Scanner keyboard = new Scanner(System.in);    
  65.  while (tryAgain)    
  66.  {  
  67.  System.out.println("Enter month, day, and year.");    
  68.  System.out.println("Do not use a comma.");  
  69.  String monthInput = keyboard.next();    
  70.  int dayInput = keyboard.nextInt();  
  71.  int yearInput = keyboard.nextInt();    
  72.  if (dateOK(monthInput, dayInput, yearInput) )  
  73.  {    
  74.  setDate(monthInput, dayInput, yearInput);  
  75.  tryAgain = false;
  76.  }  
  77.  else  
  78.  System.out.println("Illegal date. Reenter input.");  
  79.  }    
  80.  }
  81.  }
  82. Using an Overloaded Method Name
  83.  public class OverloadingDemo    
  84.  {    
  85.  public static void main(String[] args)    
  86.  {    
  87.  DateSixthTry date1 = new DateSixthTry(),    
  88.  date2 = new DateSixthTry(),    
  89.  date3 = new DateSixthTry();    
  90.  date1.setDate(1, 2, 2008);    
  91.  date2.setDate("February", 2, 2008);    
  92.  date3.setDate(2008);    
  93.  System.out.println(date1);    
  94.  System.out.println(date2);    
  95.  System.out.println(date3);    
  96.  }    
  97.  }
Add Comment
Please, Sign In to add comment