Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
  1. #21
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    And you can solve it by tomorrow. We won't give you the answer, jos gave you the answer for the helper method. Try using it then use his math, re read his post If you need.

  2. #22
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    thanks to all who is with me to solve my problem. i cannot think more right now. i shall try it later. hope i shall be able to solve this problem with your hints given

  3. #23
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    I have made a code to print the pattern as

    *****
    ****
    ***

    as follows
    Java Code:
    import java.io.*;
    public class pattern1
    {
    public static void main (String[] args) throws IOException
    {
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(read);
    int i,j,n=0,k;
    System.out.print("How many stars you want to print in first row ? ");
    n=Integer.parseInt(in.readLine());
    for (i = 0; i<n; i=i+2)
    {
    for (j=i; j<n;j++)
    System.out.print ("*");
    System.out.println();
    }
    }
    }
    but in the above code i cannot print 1 leading space in 2nd line and 2 leading space in 3rd line.
    Please help me.

  4. #24
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    This is a code as an example of Math.ceil() method. But i don't know what library class should add for this method. please help me...
    Java Code:
    public class mathceil
    {
    public static void main(String args[])
    {
    int n=31,x;
    x=Math.ceil(n/2);
    System.out.print(x);
    }
    }

  5. #25
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your question is too vague. Do you mean what do you need to import? If that's the question, nothing the math class is part of java.lang which is automatically imported for you.

  6. #26
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Your question is too vague. Do you mean what do you need to import? If that's the question, nothing the math class is part of java.lang which is automatically imported for you.
    i am getting error message as ---- "possible loss of precision".... why it is. what correction i have to do in my above code

  7. #27
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Integer division of 31/2 is 15. Play around with the code. What happens if you cast n to double? How bout if you divide by 2.0? Try both and see how they work. If it works, take a moment to think what the problem was.

  8. #28
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Integer division of 31/2 is 15. Play around with the code. What happens if you cast n to double? How bout if you divide by 2.0? Try both and see how they work. If it works, take a moment to think what the problem was.
    yes i have taken x as int. that was the wrong. i have to take x as double. thanks for hint. now i have another question. can i get output as 15 in case of 15.0 with the following code?
    Java Code:
    public class mathceil
    {
    public static void main(String args[])
    {
    int n=31;
    double x;
    x=Math.ceil(n/2);
    System.out.print(x);
    }
    }

  9. #29
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can with a cast, I'll let you figure out where the cast goes

  10. #30
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    You can with a cast, I'll let you figure out where the cast goes
    sorry ! cast means? i have littlebit communication problem as i am new in this forum.

  11. #31
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Cast is not a personal term, it's a have term. A cast allows you to cast one type to another. Try googling java cast.

  12. #32
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Cast is not a personal term, it's a have term. A cast allows you to cast one type to another. Try googling java cast.
    is it type casting?

  13. #33
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by arunjib View Post
    sorry ! cast means? i have littlebit communication problem as i am new in this forum.
    There is no need to cast anything because there is no need to use doubles. All you have to do is this:

    Java Code:
    for (int i= 0; i < rows; i++) {
        print i spaces;
        print 2*(rows-i)-1 stars;
        print a new line;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #34
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    There is no need to cast anything because there is no need to use doubles. All you have to do is this:

    Java Code:
    for (int i= 0; i < rows; i++) {
        print i spaces;
        print 2*(rows-i)-1 stars;
        print a new line;
    }
    kind regards,

    Jos
    He snuck in a new question about using Math.ceiling. I should point out to the op that you should probably create a new thread when you have a new question. Maybe it is type casting, what did you find out about type casting when you googles it?

  15. #35
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    He snuck in a new question about using Math.ceiling. I should point out to the op that you should probably create a new thread when you have a new question. Maybe it is type casting, what did you find out about type casting when you googles it?
    Type Casting refers to changing an entity of one datatype into another.

  16. #36
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, perhaps that's what you are looking for. Play around with casting variables and see what happens.

  17. #37
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    He snuck in a new question about using Math.ceiling. I should point out to the op that you should probably create a new thread when you have a new question. Maybe it is type casting, what did you find out about type casting when you googles it?
    Yep, the OP should definitely start a new thread because casts have nothing to do with his triangle printing problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •