• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enter name and age

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
but what you mean why not 2.5 ?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not this?

 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 242
27
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont know how my output : just show name.family
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic