package arraysofobjects; import java.util.Scanner; public class Main { public st
ID: 3616975 • Letter: P
Question
package arraysofobjects;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner inputs = new Scanner(System.in);
String ch, sch, sc;
double sal;
chray arr[] = new chray[3];
// chray ary = new chray();
for(int i=0;i<3;i++)
{
System.out.println(" Enter number " +(i+1) +" employee's first name");
ch = inputs.next();
System.out.println(" Enter number " +(i+1) + " employee's salary name");
sal = inputs.nextDouble();
arr[i] = new chray();
arr[i].tt(ch, sal);
}
for(int i=0;i<3;i++)
arr[i].est();
}
}
class chray
{
private String stt;
private double dr;
private int kt;
private static int count = 0;
public chray()
{
count += 1;
}
public void tt(String x, double y)
{
kt = count;
stt = x;
dr = y;
}
public void est()
{
System.out.println(" The name is " + stt +" and salary is "+dr +"and ID is " +kt);
}
}
private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private final int month; // month (between 1 and 12)
private final int day; // day (between 1 and DAYS[month]
private final int year; // year
/**
* Constructor: Does bounds-checking to ensure object represents a valid date
*
* @param m represents the month between 1 and 12
* @param d represents the date between 1 and the corresponding number from array DAYS
* @param y represents the year
* @exception RuntimeException if the date is invalid
*/
public Date(int m, int d, int y) {
if (!isValid(m, d, y)) throw new RuntimeException("Invalid date");
month = m;
day = d;
year = y;
}
/**
* Is the given date valid?
*
* @param month, day and year
* @return false if month exceeds 12 or is less than 1
* @return false if day exceeds the corresponding days for a month from array DAYS
* @return false if the year is a leap year
*/
private static boolean isValid(int m, int d, int y) {
if (m < 1 || m > 12) return false;
if (d < 1 || d > DAYS[m]) return false;
if (m == 2 && d == 29 && !isLeapYear(y)) return false;
return true;
}
/**
* is y a leap year?
* @param y represents the year specified
* @return true if year divisible by 400
* @return false if year divisible by 100 and not by 400
*/
private static boolean isLeapYear(int y) {
if (y % 400 == 0) return true;
if (y % 100 == 0) return false;
return (y % 4 == 0);
}
// return the next Date
/**
* adds a day and returns a new Date object
* @return returns a new Date object adding a day
*/
public Date next() {
if (isValid(month, day + 1, year)) return new Date(month, day + 1, year);
else if (isValid(month + 1, 1, year)) return new Date(month + 1, 1, year);
else return new Date(1, 1, year + 1);
}
// is this Date after b?
/**
* compares two Date objects
* @param b Date object
* @return true if this Date is after Date b
*/
public boolean isAfter(Date b) {
return compareTo(b) > 0;
}
// is this Date a before b?
/**
* compares two date objects
* @param b Date object
* @return true if this Date is before Date b
*/
public boolean isBefore(Date b) {
return compareTo(b) < 0;
}
// comparison function between two dates
/**
* compares two Date objects
* @param b Date object
* @return 0 if this Date is the same as Date b <br>
* negative integer if this Date is earlier than Date b <br>
* positive integer if this Date is after Date b
*/
public int compareTo(Date b) {
if (year != b.year) return year - b.year;
if (month != b.month) return month - b.month;
return day - b.day;
}
//advance date by number of months
/**
* advances the date by m months (fixed a bug on 2/22/2010)
* @param m represents the months tom advance
* @return new Date object (as the originbal obj is immutable)
*/
public Date addMonths(int m){
int tm = (month + (m % 12)) > 12 ? ((month + (m%12)) % 12) : month + (m % 12);
int ty = (month + (m % 12)) > 12 ? year + (m/12) + 1 : year + (m/12);
return (new Date (tm, day, ty));
}
// return a string representation of this date
/**
* replaces the default toString od Object class
*/
public String toString() {
return month + "/" + day + "/" + year;
}
// sample client for testing
/**
* Code for testing the Date class
* @param args Array of String arguments
*/
public static void main(String[] args) {
Date today = new Date(10, 26, 2010);
System.out.println(today);
for (int i = 0; i < 10
Explanation / Answer
please rate - thanks how about something like this? I'd recommend giving you chray class more objects init. startdate, enddate etc. each of them being of typeDate import java.util.Scanner; public class Main { public static void main(String[] args) { Scannerinputs = new Scanner(System.in); Stringch, sch, sc; doublesal; intm,d,y; chray arr[]= new chray[3]; // chray ary = newchray(); for(inti=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.