 |
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

April 29th, 2008, 03:04 PM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Requesting Help
Hello. I am completely new to java and wondered if someone might help me. I just recieved an assignment in a beginning java class that I could use some guidance with. Can anyone help?
Here is the assignment. Keep in mind that I am really lost!! Thanks!!!
Assignment:
Finish this program. For extra credit add 2 methods that do calculations and display the methods and all data fields in the toString(). Add parameters in at least one of the new methods.
// HERE'S YOUR TOOLKIT USE IT !!!!
/* i = Integer.parseInt(str);
if(i==-1) break;
l = Long.parseLong(str);
f = Float.valueOf(str).floatValue();
d = Double.valueOf(str).doubleValue();
b =Boolean.valueOf(str).booleanValue();
// as you will see you do the booleans
// like the floats and doubles !!!
// c= str[0];// nope this is C or C++
c = str.charAt(0);// correct way to read a cahracter in Java
// zero is the first character of the string object.
System.out.println(i+" "+l+" "+f+" "+d+ " "+ c);
*/
import java.io.*;
class Emprec {
public static void main(String args[]);
String address;
// double gpa;
// int age;
Emprec (String name,String address)
{
try{
this.name=name;
this.address=address;
} catch(NumberFormatException errmsg)
{
System.out.println("Invalid format"+ errmsg);
this.name = "";
// this.gpa = 0.0;
// this.age = 0;
}//catch
}//Emprec constructor !!!!
String junk () {return("Happy Birthday !!!");}
public String toString()
{
return
("\n your name is "+ name+
"\n your address is "+ address+junk());
}//toString
// see if you can get the boolean to load from a text file.
// what's missing ????
//methods !!!!
// constructors !!!
}// Emprec
class ParseEmpSolHW
{
public static void main(String args[])
throws IOException
{
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
// create strings for the input data for the Emprec object
String str_name;
String str_address;
for(;;){
//READ THE FILE !!!!
str_name = inData.readLine();
if (str_name.equalsIgnoreCase("exit")) System.exit(0);
str_address = inData.readLine();
Emprec employee=new Emprec(str_name,str_address);
System.out.println(employee);
}//for
}//main
}//ParseEmpSol
// you may try this method System.exit(0);
// to exit from the program.
|

May 1st, 2008, 02:39 PM
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Did you get any more detail on what this was going to do in the assignment. At the moment it looks vaguely like its supposed to represent a student in the US. Being from the UK (and not a student) I'd need some clarification before I could help much. Even better, ask a more specific question eg. "how can I make this calculate a gpa?"
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
|

May 1st, 2008, 03:48 PM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
[quote]Originally posted by ciderpunx
Did you get any more detail on what this was going to do in the assignment. At the moment it looks vaguely like its supposed to represent a student in the US. Being from the UK (and not a student) I'd need some clarification before I could help much. Even better, ask a more specific question eg. "how can I make this calculate a gpa?"
I am not exactly sure Ciderpunx. This assignment was just handed to me. I researched this and cannot figure it out myself hence me just being completely lost. I will see if I can get you an answer and post by tonight.
|

May 1st, 2008, 08:21 PM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
From what I found this is the end result I am trying to achieve.
class Emprec {
String name;
String address;
// double gpa;
// int age;
make the name, address gpa and age be printed by the toString() method.
The toString() now only prints the name and address. Print the other 2 fields. Also, add a method of your choice that takes in a parameter
public String toString()
{
return
("\n your name is "+ name+
"\n your address is "+ address+junk());
}//toString
Thanks so much for your help Ciderpunx.
|

May 1st, 2008, 11:10 PM
|
|
// Emprec class
class Emprec {
String name;
String address;
double gpa;
int age;
//Constructor with out arguments
Emprec()
{
this.name="";
this.address="";
this.gpa=0.0;
this.age=0;
}
//Constructor with arguments
Emprec(String name,String address,double gpa,int age)
{
this.name=name;
this.address=address;
this.gpa=gpa;
this.age=age;
}
//method to output result
public String toString()
{
return ("\n your name is "+ name+
"\n your address is "+ address +
"\n your gpa is "+ gpa +
"\n your age is "+ age +junk());
}//toString
String junk() {return(" Happy Birthday !!!");}
}
class ParseEmpSolHW
{
//this is the main class which contains public static void main()
//therefore your filename must be 'ParseEmpSolHW.java'
//this will not read form a file for that you must use fileReader class
public static void main(String args[]) throws IOException
{
Emprec emp1 = new Emprec("Jomet","kerala India", 12000.50,25);
emp1.toString();
}
}
try to do the problem!!!
and ask specific questions in the way..
jomet.
`````````````````````````````````````````````````` ``````````````````````
Once you start a working on something, dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
|

May 2nd, 2008, 02:17 PM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Jomet and thank you for helping. I managed to get the first part working and added System.out.println(emp1); to print the required name GPA Address etc.
I am not sure how to do this part of the assignment though......."the toolkit just shows you how to convert one data type into another. Finally to use the NumberFormat import java.text.*; Add this to the other import."
Can you explain? Thanks!
|

May 6th, 2008, 01:38 PM
|
Registered User
|
|
Join Date: Apr 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks again Jomet and to any who replied ...........I think I found the solution to my problem.
|

May 7th, 2008, 02:06 AM
|
|
Dear friend,
Glad to know you solved your problem.
Put your solution also, if you can!!!
That will benefit the next similar person coming this path.
jomet.
`````````````````````````````````````````````````` ``````````````````````
Once you start a working on something, dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
|
|
 |