p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Java > Java and JDK > Java Basics
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 29th, 2008, 04:04 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old May 1st, 2008, 03:39 PM
Friend of Wrox
Points: 1,424, Level: 15
Points: 1,424, Level: 15 Points: 1,424, Level: 15 Points: 1,424, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 464
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old May 1st, 2008, 04:48 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old May 1st, 2008, 09:21 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old May 2nd, 2008, 12:10 AM
Friend of Wrox
Points: 601, Level: 9
Points: 601, Level: 9 Points: 601, Level: 9 Points: 601, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2005
Location: Cochin, Kerala, India.
Posts: 183
Thanks: 4
Thanked 0 Times in 0 Posts
Default

// 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old May 2nd, 2008, 03:17 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old May 6th, 2008, 02:38 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Jomet and to any who replied ...........I think I found the solution to my problem.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old May 7th, 2008, 03:06 AM
Friend of Wrox
Points: 601, Level: 9
Points: 601, Level: 9 Points: 601, Level: 9 Points: 601, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2005
Location: Cochin, Kerala, India.
Posts: 183
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems when requesting next page Vilariño Crystal Reports 1 June 19th, 2007 01:57 PM
Requesting PAGE name myself Classic ASP Basics 6 June 4th, 2006 09:23 AM
Requesting Data Input from Operator Ralph Weikert MySQL 0 May 5th, 2006 10:48 PM
Problem in requesting certificate bhupi_josh BOOK: ASP.NET Website Programming Problem-Design-Solution 0 August 5th, 2005 03:38 AM
Requesting techniques use to learn ASP. cdplayer BOOK: Beginning ASP 3.0 1 March 19th, 2004 12:59 PM



All times are GMT -4. The time now is 03:43 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc