what are the parameters needed in the constructor call below to create a Date variable whose intitial value is the 15th of March 2004????
Homework help
Collapse
X
-
Tags: None
-
3 JAVA questions - Urgent help
3 JAVA QUESTIONS THAT I NEED ANSWERING??!?!
Enter the parameters needed in the constructor call below to create a Date variable whose intitial value is the 15th of March 2004.
Date d = new Date( ____________ );
--------------------------------------------------------------------------------
[2] There is a standard class called DateFormat which (among other things) lets you convert dates to various different output formats.
Use it to write a method called convert which converts a Date to a String in the form dd-mmm-yyyy:
public String convert (Date d) {
...
}
For example, when d is a Date variable representing the 25th of December 2006,
String s = convert(d);
should set s to the string "25-Dec-2006".
Enter your answer (just your convert method) below:
--------------------------------------------------------------------------------
[3] Write a condition for the if statement below which will be true when the Date d1 is not later than the Date d2:
if ( _______________ _____ ) {
...
} -
I don't have Java installed anymore, but it may be helpful to look at the Java API for DateFormat. I noticed a method called parse(string s) that may help you with the first question. For number 2, look at the format(Date d, StringBuffer sb, FieldPosition fp) method, and maybe getNumberFormat ().
I assume Date(0) is equivalent to midnight Jan 1, 1979, just as the new year began. The time is given in milliseconds, so then every (1000*60*60*24* 365.25...=31557 600000) is approximately equivalent to a year. There are algorithms to calculate how many days are in between two dates. Finally, if each date is represented as the number of milliseconds then you could simply compare these two numbers that represent the date. I am assuming the less than operator is already implemented for Date.Comment
-
The easiest thing would be to parse the date, using DateFormat.pars e(string source), and let Java do all those calculations so Java figures out a time in ms for you. However, here are the calculations that go on behind the scenes, in case you want to understand.
Leap yearsCalculationsCode://int year; bool leap; if((year % 400) == 0) // If year is divisible by 400 leap = true; if((year % 100) == 0) // If year is divisible by 100 leap = false; if((year % 4) == 0) // If year is divisible by 4 leap = true;
So (26*365 + 6) = 9490+6 =9496 days. // 6 leap years, 1980, 84, 88, 92, 96, 2004, but not 2000.
9496 days * (24 hrs/day) = 227904 hrs.
227904 hrs * (60 min/hr) = 13674240 min.
13674240 min * (60 sec/min) = 820454400 sec.
820454400 sec * (1000ms/sec) = 820454400000 ms.
The 26 years = 820454400000 ms.
We still need to count Jan, Feb, and part of March.
Jan = 31 days, Feb = 28 days (we already counted the leap year since 2000 was not a leap year) and Mar = 15 days.
74 days * 24 hrs/day * 60 min/hr * 60 sec/min * 1000 ms/sec = 6393600000ms.
820454400000 ms + 6393600000 ms = 826848000000 ms.
So, Date(8268480000 00) should give you Mar, 15, 2004 right at midnight. If you want 4:30 PM, you would have to add
(16hrs(60 min/hr) + 30 min)*60sec/min*1000ms/sec = 59400000 ms.
Then the date would be 826848000000 ms + 59400000 ms = 826907400000 ms, or Date(8269074000 00).Comment
-
[1] See the other postOriginally posted by jamieexley3 JAVA QUESTIONS THAT I NEED ANSWERING??!?!
Enter the parameters needed in the constructor call below to create a Date variable whose intitial value is the 15th of March 2004.
Date d = new Date( ____________ );
--------------------------------------------------------------------------------
[2] There is a standard class called DateFormat which (among other things) lets you convert dates to various different output formats.
Use it to write a method called convert which converts a Date to a String in the form dd-mmm-yyyy:
public String convert (Date d) {
...
}
For example, when d is a Date variable representing the 25th of December 2006,
String s = convert(d);
should set s to the string "25-Dec-2006".
Enter your answer (just your convert method) below:
--------------------------------------------------------------------------------
[3] Write a condition for the if statement below which will be true when the Date d1 is not later than the Date d2:
if ( _______________ _____ ) {
...
}
[2]
GregorianCalend ar c = new GregorianCalend ar(2006, 11, 25);
Date date = c.getTime();
SimpleDateForma t sdf = new SimpleDateForma t("dd-MMM-yyyy", Locale.US);
result = sdf.format(date );
[3]d1.compareTo(d2 ) <= 0Comment
-
Programmings questions, Novice please help
[1] Given a list of integers and an integer variable declared like this:
List<Integer> list;
int max;
and assuming that some values have been added to the list, write a loop which finds the largest value in list and stores it in max.
--------------------------------------------------------------------------------
[2] A bank account is stored as a Map which maps the names of the account holders to a value representing the current balance of their account:
Map<String,Inte ger> bank;
Given a String variable declared like this:
String rich = null;
write a loop which will store the name of the person with the most money in their account in rich.
--------------------------------------------------------------------------------
[3] Using the same bank account as in the previous question, and a list of strings:
Map<String,Inte ger> bank;
List<String> names;
write a loop which fills the list names with the names of all account holders whose accounts are overdrawn, i.e. the balance of their account is less than zero.
------------------------------------------------------------------------------------
[4] Write a method called convertDouble as follows:
public double convertDouble (String value) {
...
}
This method should convert its parameter (a string like "3.1415") to the corresponding value of type double. If the string supplied is not a valid number, it should return 0.0 as its result. Note that you can use the method Double.parseDou ble() to do the hard work for you.
Insert your convertDouble method in the space below:
--------------------------------------------------------------------------------
[5] Modify your convertDouble method to ignore any extra characters at the end of the string, so that convertDouble(" 3.1415xxx") will produce the value 3.1415 as its result.
Insert your convertDouble method in the space below:
--------------------------------------------------------------------------------
[6]
Write a method called countChars which takes an InputStream as a parameter, reads the stream and returns the number of characters it contains as an int. Any IOExceptions which might occur in the method should be passed back to the method's caller.
Insert the code for your method in the space below:
------------------------------------------------------------------------------
[7]
Using the countChars() method from the previous answer, write a method called fileSize() declared as follows:
public int fileSize (String name) {
...
}
which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.
Insert the code for your method in the space below:Comment
-
-
What is the loop which finds the largest value in list and stores it in max?
Given a list of integers and an integer variable declared like this:
List<Integer> list;
int max;
and assuming that some values have been added to the list,
What is the loop which finds the largest value in list and stores it in max?Comment
-
-
Originally posted by jamieexleyGiven a list of integers and an integer variable declared like this:
List<Integer> list;
int max;
and assuming that some values have been added to the list,
What is the loop which finds the largest value in list and stores it in max?Code:public static void main(String[] args) { List<Integer> list = new LinkedList<Integer>(); int max = -999; list.add(1); list.add(12); list.add(2); list.add(11); for (int i : list) { if (i > max) { max = i; } } System.out.print("Max is : "); System.out.println(max); }Comment
-
-
What is the loop which will store the name of the person with the most money in....
A bank account is stored as a Map which maps the names of the account holders to a value representing the current balance of their account:
Map<String,Inte ger> bank;
Given a String variable declared like this:
String rich = null;
How is the loop which will store the name of the person with the most money in their account in rich written?Comment
-
How do write a method called countChars which takes an InputStream.... .........
I was given this, can anyone shed some light on it????
How do write a method called countChars which takes an InputStream as a parameter, reads the stream and returns the number of characters it contains as an int. Any IOExceptions which might occur in the method should be passed back to the method's caller.Comment
Comment