• Post Reply Bookmark Topic Watch Topic
  • New Topic

enter name and age  RSS feed

 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare a double variable, call it and give it the value 2.5 and then print it.

Create a small program that asks the user to enter name and age and the program will display them back


Create a string array of size 2 and name it “myName” and in index 0 write your name and index 1 write your family name, and then print on the screen:
My name is [index zero of the array] [index one of the array].


Write a Java program that takes from the user an integer number and checks if the number is positive or negative and display that on the screen.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like 4 different programs, so start from the first one:

"Declare a double variable, call it mydouble and give it the value 2.5 and then print it. "

What have you managed to write and what problems have you had?
 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That looks like 4 different programs, so start from the first one:

"Declare a double variable, call it mydouble and give it the value 2.5 and then print it. "

What have you managed to write and what problems have you had?


double number=5;
double number2=2;
double answer;
answer=number/number2;
System.out.println("mydouble="+answer);
}
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Where is remaining part? Do you know how to write a class? If you don't know then you may start from The Java™ Tutorials
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

It looks a hard way to do things, dividing 5 by 2 (if you had done that without declaring the variables as double type, you would have got 2). Why not simply say 2.5?
I removed some unused code tags from your posts.
 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
but what you mean why not 2.5 ?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not this?

 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top tip:
When doing these sorts of exercises follow the instructions as close as possible.
They're not there to try and trick you or anything, or to see strange and wonderful ways of doing things.
Simply follow what they say.

So, in this case, "Declare a double variable, call it mydouble and give it the value 2.5 and then print it. "
There's no mention in there of number/number2/answer variables.
There's also no mention of any extra text with the println.

Avoid adding extra bits if you can.  And in this case you can.
 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for advice it's really helpful to me .
 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amnah AlMajid wrote:Declare a double variable, call it and give it the value 2.5 and then print it.

 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a small program that asks the user to enter name and age and the program will display them back
import java.util.Scanner;


 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use code tags, the opening and closing tags should surround the code.  Click the link for more information.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amnah ali wrote:Create a small program that asks the user to enter name and age and the program will display them back

How would you improve your program if'd like to enter and see displayed my full name: 'Liutauras Vilda' ?

While your program most likely does what requirements ask, pay attention to code formatting, variables naming.

1. 'Name' variable starts with an upper case for some reason, while age with lower case. Such variable supposed to start with lower case as per Java coding guidelines. ex: name, fullName, firstName, surname, age, iceAge...
2. Have a look at your print statements. You start your sentences with lower case, while they probably better would start with an upper cases. Also check how first print statement text looks like - it seems there is a space character in front, why?
3. Put some spaces around '='. Not doing so it reduces code readability.

It is very important to get good habits from the beginning as later could be difficult to amend them.
 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Create a string array of size 2 and name it “myName” and in index 0 write your name and index 1 write your family name, and then print on the screen:
My name is [index zero of the array] [index one of the array].

public class NewClass1 { 
   
   
    public static void main(String[] args) {
      
        String [] AM = {"Name","FamilyName"};

        for(int n=0;n<AM.length;n++){
            System.out.print(AM[n]);
           
       
    }
   
}
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please add code tags to your posts. That link explains how the tags work; you didn't have them right but I corrected the post and it looks much better
In the last post, you didn't follow the instructions. It tells you what to call the array, but you used a different name. Don't start such names with Capital Letters. Why did you write that loop? It doesn't say to write a loop, but to print the two elements of the array.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When ran, the output of this code is the following:
NameFamilyName

But the requested output is:
My name is [index zero of the array] [index one of the array]

Which in your context would be
My name is Name FamilyName


So instead of looping through, you should access those fields directly and put it in the format requested. (Also, they probably want your actual name and last name in the array, not just "{Name}, {FamilyName}")

 
Amnah ali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont know how my output : just show name.family
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your post earlier you know how to concatenate strings:


So that's what you need here to get the output:
My name is Name FamilyName

Except, instead of Name and age variables you will be using the array, index 0 and 1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!