Java assignment help?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 9385 Views - Last Post: 31 March 2006 - 10:07 PM Rate Topic: -----

#1 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Java assignment help?

Posted 27 March 2006 - 01:46 PM

I have an assignment thats a little out of my skill range I'm a noobie and not sure were to start can some one help get get on track?

Here is the assignment

Assignment Requrirements:
Create a program that takes a name and a number as command line String arguments. Have a method in the class that returns whether that number is equal to the length of the name. The program should:
1. Capitalize the first letter of the name.(pg272)
2. Output the capitalized name to the screen
3. Output whether the number was equal to the length of the name.
HINT: The number must be converted to int (pg 270-271)
Example: >java ChNineAsgn danica 7
The name capitalized is: Danica
The number is not equal to the length of the name.

any help will be well recieved thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Java assignment help?

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 27 March 2006 - 05:24 PM

The site has a policy by which we are more than happy to help users with their academic assignments, but we do require that some good faith effort is shown by the user. If you could please post the code you have written in an attempt to solve this assignment, we would be pleased to provide some guidance.


Having said that, here is some base code to get you started:
public class cmdLineArgs{
    public static void main (String[] args) {
        for (int i = 0; i < args.length; i++)
            System.out.println(args[i]);
    }
}


As you can see, that code will print out any command line arguments that have been passed to the program. Now you need to
  • Access each command line argument supplied
  • Manipulate where necessary
The assignment description serves admirably as the pseudocode...give it a try, we'll be glad to help out.
Was This Post Helpful? 0
  • +
  • -

#3 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 27 March 2006 - 06:45 PM

Heres what i have so far

/**
 * 
 *
 */
public class cmdLineArgs
{
  public static void main (String[] args) 
  { 
    
    String firstName = args[0];
    
    System.out.println("Enter a name and a number");
    System.out.println(" + firstname + ");
    
    char c = Character.toUpperCase('a');
    char c1 = firstName.charAt(0);
    
    firstName = Character.toUpperCase¬© + firstName;
    
    for (int i = 0; i < args.length; i++)
           System.out.println(args[i]);
   }
}



I'm getting a run time error it says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at cmdLineArgs.main(cmdLineArgs.java:10)
Press any key to continue...

I'm still working on it I'm new and having some trouble getting the syntax and layout of writing my own programs thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 27 March 2006 - 08:07 PM

You say you are getting a runtime error...what arguments are you passing the program when running it? That error may be ocurring because you are passing no arguments to the program, but you are trying to access the argument list. Try the following...does it compile and run for you (if you call it as java cmdlin jason 5)?
import java.io.*;

public class cmdlin {
    public static void main (String[] args) {
       String fname = args[0];
       int num = Integer.parseInt(args[1]);
       System.out.println(fname);
       System.out.println(num);
    }
}



Here is a further hint...for capitalization, check out the substring method of the string class. For the length, compare the length of the string object to the number entered by the user.
Was This Post Helpful? 0
  • +
  • -

#5 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: Java assignment help?

Posted 28 March 2006 - 02:40 AM

You're probably getting your error on
Character.toUpperCase + firstName

that is adding a string to your method :P




Here is what you need to do:
1. Capitalize the first letter of the name.(pg272)
- Substring the first character out to its own string
- Make it uppercase
2. Output the capitalized name to the screen
- Substring everything but the first name out into its own string
- Concatenate the first character(now in uppercase) with the string that is everything but the first char.
3. Output whether the number was equal to the length of the name.
- just use basic logic to check if the number is greater than the length :P



This is a decent site for learning about substring :]
http://mindprod.com/.../substring.html
Was This Post Helpful? 0
  • +
  • -

#6 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 28 March 2006 - 01:12 PM

I decided to try and start by Capitalizing the first character of the input text and then go from there here's what I'm working on now and thanks for your help.
[CODE]
/**
*
*
*/
import java.io.*;

public class cmdLineArgs
{
public static void main (String[] args)
{
EasyReader console = new EasyReader();
System.out.print("Enter a name:");
String firstName = console.readLine();
System.out.println("" + firstName +"" );

char c1 = Character.toUpperCase('a');

String firstname = console.readLine();
char c = firstName.charAt(0);
firstName = Character.toUpperCase© + firstname.substring(1);
}
}

Here is the error

Enter a name:hello
hello

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -1
at java.lang.String.substring(String.java:1768)
at java.lang.String.substring(String.java:1735)
at cmdLineArgs.main(cmdLineArgs.java:20)
Press any key to continue...


I think I'm having trouble telling it how to Capitilize the first letter (char c1 = firstName is a problem not sure how it should be?

This post has been edited by chet123: 28 March 2006 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: Java assignment help?

Posted 28 March 2006 - 01:21 PM

[admin edit]sorry Sontek, you're solution is correct, but we prefer not to give complete solutions for homework questions...better to guide the user...[admin edit]

This post has been edited by Amadeus: 28 March 2006 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 28 March 2006 - 01:30 PM

The program you have designed does not take command line arguments, as per your specifications. You need to provide the arguments in the command line, compare the length of the entered name to the number provided, output the result of the comparison, and capitalize the first letter of the word. The program only really requires a few lines...I will provide you with ideas the piece components, and then we can work on putting it together.

you don;t really need any variables for this assignment, but we'll use them to make things clear.

You should have a string that holds the name, which is supplied as the first argument...the one in array index 0, correct?
String fname = args[0];


You also need an integer that is the number supplied as the second argument...keep in mind that a command line argument is text, so you will have to convert it to a number:
int num = Integer.parseInt(args[1]);


Compare the number (num) to the length of the name (fname)...strings have a property called length, which you can access for the comparison...the length of the name is fname.length().

Based on the result of the comparison, you can output the appropriate message.

The String Class has a method called substring()...this can be used to get portions of the string. you want to capitalize the first letter
fname.substring(0,1).toUpperCase


This will return the capitalized first letter (please see link for how the substring method works)

You then want to display the capital letter, then the rest of the word without the capital letter.
fname.substring(0,1).toUpperCase() + fname.substring(1)


the second part will return the string, minus the first letter.

Then, it's just a matter of putting it together. :)
Was This Post Helpful? 0
  • +
  • -

#9 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 28 March 2006 - 02:20 PM

I have a question is there more that one way to complete this assignment? And if there is, I must be going about it the hard way :P

This post has been edited by chet123: 28 March 2006 - 02:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 28 March 2006 - 03:10 PM

There are several ways to complete it...it can actually be done in about 5 lines of code. You can step through it piece by piece or optinize by simply performing the steps on the actual supplied arguments...it's all up to you! :)
Was This Post Helpful? 0
  • +
  • -

#11 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 28 March 2006 - 03:56 PM

I don't understand how you are able to enter the name and number after running the program

this is how you ask for it at

System.out.print("enter a name and a number")

should this be first in the program?

and do i need 


EasyReader console = new EasyReader();

in order to get the program to take input?

System.out.println(" + firstname + ");

can I add toUpperCase in this line

[CODE](" + firstName.toUpperCase.substring(1);
Was This Post Helpful? 0
  • +
  • -

#12 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 28 March 2006 - 09:50 PM

Thanks to all of your help I've gotten closer to finishing the code for this assignment thank's here is what i have working so far, again thanks.




public class Input
{
  public static void main(String[] args)
  {
    EasyReader console = new EasyReader();
    System.out.print("Enter a Name");
    String firstName = console.readLine();
    char c = firstName.charAt(0);
    firstName = Character.toUpperCase(c) + firstName.substring(1);
    System.out.println("" + firstName + "");
  }
}


This post has been edited by chet123: 28 March 2006 - 09:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 28 March 2006 - 10:23 PM

You are progressing, but I should point out that your assignment is not to ask the user for the name and length...it is, as specified in the first post, to accept those as command line arguments, is it not?
Was This Post Helpful? 0
  • +
  • -

#14 chet123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-March 06

Re: Java assignment help?

Posted 28 March 2006 - 10:30 PM

Are you saying that the program needs only to printout a name and number I use in the program?

If so than I'm making it harder than it has to be.

So I could use any name and number in the program because I thought that it had to have a user's input after running?

This post has been edited by chet123: 28 March 2006 - 10:52 PM

Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Java assignment help?

Posted 29 March 2006 - 04:44 AM

Quote

Assignment Requirements:
Create a program that takes a name and a number as command line String arguments.


The description above means that you must supply the name and length at runtime...like so:
java myprogram charles 7


Unless there is more to the assignment, you do not need to prompt the user for the information.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2