what are the parameters needed in the constructor call below to create a Date variable whose intitial value is the 15th of March 2004????
32 7116
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...=31557600000) 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.
The easiest thing would be to parse the date, using DateFormat.parse(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 years - //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;
Calculations
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(826848000000) 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(826907400000).
Date d = new Date(104, 2, 15);
104 + 1900 = 2004
2 is for March(0 based)
15 for 15 ;-)
But the class Date is deprecated (One of the thing i will nerver understand in Java), you should use the GregorianCalendar instead
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 ( ____________________ ) {
...
}
[1] See the other post
[2]
GregorianCalendar c = new GregorianCalendar(2006, 11, 25);
Date date = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
result = sdf.format(date);
[3]d1.compareTo(d2) <= 0
[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,Integer> 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,Integer> 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.parseDouble() 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:
I think you don't need a forum but a Java training ;-)
If you are developping a system with money and account, I think you should use BigDecimal (Fix capacity) instead of double (rouding error...)
I know i need training, but I need to resolve these questions? can you help please
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?
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?
-
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);
-
}
-
Thank u very much, appreciate that.
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,Integer> 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?
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.
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,Integer> 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?
- Map<String, Integer> bank = new Hashtable<String, Integer>();
-
String rich = null;
-
Integer richBalance = new Integer(0);
-
-
bank.put("aaa", new Integer(1020));
-
bank.put("bbb", new Integer(2333));
-
bank.put("ccc", new Integer(1356));
-
-
for (String account : bank.keySet())
-
{
-
if (bank.get(account) > richBalance)
-
{
-
rich = account;
-
richBalance = bank.get(rich);
-
}
-
}
-
System.out.println("Max is : " + rich + " (" + richBalance + ")");
-
It's my last for that day!
Just 1 more! Please
(3rd & Final)
Using the same bank account as in the previous question, and a list of strings:
Map<String,Integer> bank;
List<String> names;
I need to 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???
Just 1 more! Please
(3rd & Final)
Using the same bank account as in the previous question, and a list of strings:
Map<String,Integer> bank;
List<String> names;
I need to 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???
-
Map<String, Integer> bank = new Hashtable<String, Integer>();
-
String rich = null;
-
Integer richBalance = new Integer(0);
-
List<String> names = new LinkedList<String>();
-
-
bank.put("aaa", new Integer(1020));
-
bank.put("bbb", new Integer(2333));
-
bank.put("ccc", new Integer(-1356));
-
bank.put("ddd", new Integer(1356));
-
bank.put("eee", new Integer(-56));
-
-
for (String account : bank.keySet())
-
{
-
if (bank.get(account) > richBalance)
-
{
-
rich = account;
-
richBalance = bank.get(rich);
-
}
-
if (bank.get(account) < 0)
-
{
-
names.add(account);
-
}
-
}
-
System.out.println("Max is : " + rich + " (" + richBalance + ")");
-
System.out.println("Names are : " + names);
-
It is easy as you can see !!!
In fact it is the first time I use Generics in Java (I remembered ugly code and response time in C++ with templates) and it's quite good. Thanks for the lesson
I don't know how list is defined, but this may give you an idea of how to do it. I am assuming there are list.length() elements in the list, and list.entry(i) returns the ith element. Similarly for map. I will give you an idea for #2, and some suggestions for the others. Number 2 - max = map.entry(0).getInt(); // since list is not empty.
-
for(int i = 0; i < map.length(); i++)
-
{
-
if(max < map.entry(i).getInt())
-
{
-
max = map.entry(i).getInt());
-
rich = map.entry(i).getStr());
-
}
-
}
#1. Just like #2, only you don't worry about the string rich. Also it's a list instead of a map.
#3. Change the if statement. It's condition is given to you. The code to execute should simply put the person's name into a list.
The others seem just as trivial. Give it a go yourself, and if you have any trouble, post your code, what's wrong with it, and what you can't figure out. Then we'll look at it and see.
Java API for BufferedInputStream. Become friends with the Java API. I don't have Java to test this, but it should work. Also, by not catching the exception, it is returned as requested. - public int countChars(InputStream is) throws IOException
-
{
-
int count = 0;
-
BufferedInputStream bis = new BufferedInputStream(is);
-
while(bis.read() != -1) // -1 is returned when end of input is reached.
-
{
-
count++;
-
}
-
return count;
-
}
I don't know how list is defined, but this may give you an idea of how to do it. I am assuming there are list.length() elements in the list, and list.entry(i) returns the ith element. Similarly for map. I will give you an idea for #2, and some suggestions for the others. Number 2 - max = map.entry(0).getInt(); // since list is not empty.
-
for(int i = 0; i < map.length(); i++)
-
{
-
if(max < map.entry(i).getInt())
-
{
-
max = map.entry(i).getInt());
-
rich = map.entry(i).getStr());
-
}
-
}
#1. Just like #2, only you don't worry about the string rich. Also it's a list instead of a map.
#3. Change the if statement. It's condition is given to you. The code to execute should simply put the person's name into a list.
The others seem just as trivial. Give it a go yourself, and if you have any trouble, post your code, what's wrong with it, and what you can't figure out. Then we'll look at it and see.
An other way is to use Java 5.0 Generics and loop Other thread
All of jaime's questions got merged into one homework help thread.
I need some help on this. I did this code but now need to be updated. I really dont understand what are the benefit. How do I do? Thank you so much for any help.
Ruth
I have the code below how do I do to update the new method:
int count = ((Integer)(orderMap.get(key))).intValue();
currentOrder.put(unknownItem, new Integer(count));
That is the code to be updated.
public class ToysInventory {
private String[] toysOrdered = {"bear", "train", "car", "ball", "doll",
"ball", "train", "doll", "game", "train",
"bear", "doll", "train","car", "ball",
"bat", "glove", "bat", "b", "doll", "bear",
"ball", "doll", "bat", "car", "glove",
"train", "doll", "bear" };
private String[] toysInventoryList = {"ball", "bat", "bear", "car", "doll",
"game", "glove", "playstation", "train" };
private int InventoryCounter[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public static void main(String[] args) {//BEGIN main()
ToysInventory toys = new ToysInventory();
toys.countToys();
System.exit(0);
}//END main()
private void countToys() {
int temp = 0;
int orderedIndex = 0;
int inventoryIndex = 0;
for (inventoryIndex = 0; inventoryIndex < toysInventoryList.length; inventoryIndex++){
for (orderedIndex = 0; orderedIndex < toysOrdered.length; orderedIndex++) {
if (toysOrdered[orderedIndex] == toysInventoryList[inventoryIndex]) {
temp = temp + 1;
}//END if
InventoryCounter[inventoryIndex] = temp;
}
temp = 0; //reset counter value
}
System.out.println("[order status]");
for (temp = 0; temp < InventoryCounter.length; temp++){
// (*) before any item for which the customer has requested
// a quantity of 5 or more.
if (InventoryCounter[temp] >= 5){
System.out.print("*");
}
else {
System.out.print(" ");
}
System.out.println(toysOrdered[temp] + " = " + InventoryCounter[temp]);
}
}//END countToys()
}//END ToysInventory
for question
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.
i used this answer
for (int i : list)
{
if (i > max)
{
max = i;
}
}
System.out.println(max);
}
and i get these errors
Main.java:36: illegal start of type
for (Integer n : list) {
^
Main.java:48: <identifier> expected
}
^
Main.java:49: 'class' or 'interface' expected
}
^
Main.java:49: 'class' or 'interface' expected
}
^
4 errors
any ideas in what im doing wrong?
for question
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.
i used this answer
for (int i : list)
{
if (i > max)
{
max = i;
}
}
System.out.println(max);
}
and i get these errors
Main.java:36: illegal start of type
for (Integer n : list) {
^
Main.java:48: <identifier> expected
}
^
Main.java:49: 'class' or 'interface' expected
}
^
Main.java:49: 'class' or 'interface' expected
}
^
4 errors
any ideas in what im doing wrong?
Hey joej231,
I think it would have been helpful if you had posted the complete code because without the code the errors are of no help.
Anyways the following code finds the max given a list of numbers(that is what you are trying to do..right?). Hope it solves your problem.
[ no code spoonfeeding alllowed -- mod ]
First of all, joej should've started his/her own thread for the problem and not hijack
a years old thread. Second, we don't spoonfeed source code.
kind regards,
Jos
[ no code spoonfeeding alllowed -- mod ]
First of all, joej should've started his/her own thread for the problem and not hijack
a years old thread. Second, we don't spoonfeed source code.
kind regards,
Jos
I'm confused, is this the real JosAH or someone who is saying some thing that JosAH would say? Because I think also he would have said be sure to use CODE tags.
~mona
I'm confused, is this the real JosAH or someone who is saying some thing that JosAH would say? Because I think also he would have said be sure to use CODE tags.
~mona
I almost never say that, I simply insert those tags myself and add an 'edit' line ;-)
kind regards,
Jos ( and don't forget the CODE tags folks! ;-)
I almost never say that, I simply insert those tags myself and add an 'edit' line ;-)
kind regards,
Jos ( and don't forget the CODE tags folks! ;-)
But apparently not in this case? lol
~mona
[ no code spoonfeeding alllowed -- mod ]
First of all, joej should've started his/her own thread for the problem and not hijack
a years old thread. Second, we don't spoonfeed source code.
kind regards,
Jos
Point noted :)
I had read about not spoonfeeding source code on the forum but I felt he had written most of the logic correctly and was probably making a mistake in initialization/syntax, so I posted the code.
Anyways from now on I'll avoid posting code.
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.
This is what i have done :
public int fileSize(String name) throws IOException
{
try{
int count = 0;
File file = new File("name");
Scanner scan = new Scanner(file);
while( file.exists())
{
count++;
}
return count;
}
catch (IOException e) {
return -1;
}
}
error: cannot find the class scanner.
Any suggestions would be much appreciated?
As much as I hate to implicitly condone thread necromancy, did you import java.util.Scanner?
same question ..
public int fileSize(String name) throws IOException
{ try {
InputStreamReader isr = new InputStreamReader(new FileInputStream("name"));
int charLength = 0;
int c;
while ((c = isr.read()) != -1)
charLength++;
return(charLength);
}
catch (Exception e) {
return -1;
}
}
error : Test 1 succeeded (0 marks):
The compilation was successful
Test 2 failed (exit code = 1):
The output should have been:
countChars
This is what was actually produced:
public int fileSize(String name) throws IOException
{ try {
InputStreamReader isr = new InputStreamReader(new FileInputStream("name"));
int charLength = 0;
int c;
while ((c = isr.read()) != -1)
charLength++;
return(charLength);
}
catch (Exception e) {
return -1;
}
}
Test 3 failed (exit code = 1):
Your converted value was -1
It should have been 1446
Try again!
Test 4 failed (exit code = 1):
Your converted value was -1
It should have been 414
Try again!
Test 5 succeeded (1 mark):
Success!
help??
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tjshadyluver |
last post by:
ok i have gotten this damn project almost done but i am frustrated on
this one step. getting 18-35 together and 36-50 and so on. here is my
code how can i get them combined in one line instead of...
|
by: N3TB1N |
last post by:
Let me try again. I could use some help with this assignment, even
though my teacher does not grade assignments.but because I need to
know this stuff for a test very soon, but haven't been in...
|
by: spawn |
last post by:
but I've been struggling with this for far too long and I'm about to
start beating my head against the wall.
My assignment seemed simple: create a program that will cacluate the
running total of...
|
by: mastern200 |
last post by:
I need some homework help with an assignment due wed. I need to make a program where in this program calculates the average of a set of test scores.
The program will ask the user how many scores...
|
by: karafire2003 |
last post by:
I was wondering if someone can help me with my homework. here's the assignment:
a. Write a C program that has a declaration in main() to store the string "What's for lunch?" into an array named...
|
by: itgetsharder |
last post by:
Hey, i was wondering if anyone could help me.
i have two questions that i cannot complete for a homework assignment:
This method should convert its parameter (a string like "3.1415") to the...
|
by: garyrowell |
last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received.
The program
This week you are going to write three...
|
by: alireza6485 |
last post by:
I need to write a C# program that asks for a pattern and for each pattern beeps one and goes silence for 1 second.
This is my code:
for (int i=0;i< pattern; i++)
{
...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |