• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

if statement

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

guys i am trying to use the pytagoras formula in java and to calculate distance.
with the resualt i need to calculate the price of a ticket (each 100 km is 50$)
i am trying 350 (point 1) and 2471 (point 2) the resualt after the formula is 2495.
then i was about to use if statement on the resualt distance somthing like that:
flightPrice = distance/100*50;
if (distance%100!=0){
flightPrice+50)
}
(i cannot make the System.out.println becuase i need to calculate the creditcard numbers)
 
Sheriff
Posts: 7123
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
Welcome to the Ranch, Jenia Levin!

I'm sorry, what is the question exactly?
 
Saloon Keeper
Posts: 9715
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
flightPrice = distance/100*50;
If you have this formula then why would you need an 'if' statement?
 
Jenia Levin
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLEASE TELL ME WHAT DO YOU THINK GUYS.
becuase if the distance is less then 100 kilometers.
but i have figure out how i can make it.
//This program calculate the distance (using pytagoras formula),
// calculating the price per 100 kilometers. using user input.
// checking if the credit card is valid(user input)
import java.util.Scanner;
public class TravelAgency
{
   public static void main(String[]Args)
   {
       Scanner scan = new Scanner(System.in);
       System.out.println("Hello, Please enter your" +" "+
           "destination in two integers:");
       //variables and variables for calculation.    
       final int pricePer = 50;
       final int distanceInKm = 100;
       int userPoint1, userPoint2, cerditCard,
       destination ,flightPrice,x,y, a, b;
       //getting userinput for the formula.
       userPoint1 = scan.nextInt();
       userPoint2 = scan.nextInt();
       //calculating the userinput with the formula.
       destination = (int)Math.sqrt((userPoint1*userPoint1)+
           (userPoint2*userPoint2));
       //calculation the price + if the kilometer is less then 100 or 0.
       flightPrice =  ((destination/distanceInKm)*pricePer);
       a= destination%distanceInKm;
       if (a!=0)
       {
           System.out.println("The price of the flight is:"+" "+
               (flightPrice+pricePer));
       }
       if (userPoint2==0&&(userPoint1==0)){
           System.out.println("The price of the flight is 0"+ " "+
               "but we still will check your credit card");}
       //getting credit card number from user.        
       System.out.println("Please enter your credit card number:");
       cerditCard = scan.nextInt();
       //checking if the credit card is valid.
       //1-6 numbers+ first number not 0
       // and checking if first5Numbers%7 == last number.
       x = cerditCard/10;
       y = cerditCard%10;
       b = y%7;
       if (cerditCard<999999&&cerditCard>99999&&(b==y))
       {
           System.out.println("bon voyage");
       }
       else
       {System.out.println("Your credit card is invalid");}
   }
}// end of program.
 
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

Jenia Levin wrote:PLEASE TELL ME WHAT DO YOU THINK GUYS. . . .

There is no need to shout.

Please revise the rules about integer arithmetic, and find out how this works:-Have yoiu been told to do the calculations for Pythagoras, because this method can do all the work for you. But you are setting the start points and end points for your flights have the two coordinates the same.
I see you have tried the code button and not quite got it right: have a look here. This is what your code would have looked like with code tags:-I am pleased to see how you have indented the code and avoided long lines .
Unfortunately your code shows some problems.
  • Don't declare all your variables on one line; that doesn't make your code easy to read.
  • Your variables have poor names, a, x, y, whose meaning isn't obvious. Youi don't appear to be using x anywhere. Names like destination don't say what the variable means.
  • Put spaces around your binary operators, e.g. % /
  • I know people are told to insert comments, but if they only say what is obvious from looking at the code, comments are of little use.
  • You have much too much code in the main method. 99% of that code belongs elsewhere.
  • Please explain the logic of lines 40‑43. That is where comments explaining the logic would have been useful.
  •  
    Carey Brown
    Saloon Keeper
    Posts: 9715
    79
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A "point" would require both an x and a y component. You could use x1, y1 for point 1, and x2, y2 for point 2.
     
    Sheriff
    Posts: 17288
    299
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This illustrates how critical it is to choose good names for things you’re trying to represent in your program.

    In your program:

    The Pythagorean theorem describes the relationship of the three sides of a right triangle: c² = a² + b²

    So, the names userPoint1 and userPoint2 are misleading because they really represent the lengths of the a and b sides of a right triangle and destination represents the length of the hypotenuse or the c side.

    Because the names are misleading, the discussions start heading off on tangents like x and y coordinates to represent points. This just creates more noise that adds to the confusion. All because the wrong names were chosen.

    If you had done something like this instead, the intent would have been clearer:
     
    Carey Brown
    Saloon Keeper
    Posts: 9715
    79
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, I see. If you have any remainder then bump it up by another 50.
    You could achieve the same thing with this formula which will work because you're dealing with integer division.

    I agree with the current discussion on selecting proper names for things. Your choices of: "point", "x", and "y", for instance have specific meaning in mathematics which has probably led me to make incorrect assumptions about your requirements. Until you clean this up I will remain in some doubt.
     
    Jenia Levin
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    guys i have change my code little bit =)
    dont forget i am a begginer from each mistake i am learning my leasson =)

    //This program calculate the distance (using pytagoras formula),
    // calculating the price per 100 kilometers. using user input.
    // checking if the credit card is valid(user input)
    import java.util.Scanner;
    public class TravelAgency
    {
       public static void main(String[]Args)
       {
           Scanner scan = new Scanner(System.in);
           System.out.println("Hello, Please enter your" +" "+
               "destination in two integers:");
           //variables and variables for calculation.    

           int userPoint1, userPoint2, cerditCard,
           destination ,flightPrice,distanceInKm,
           pricePer, num1,num2, num3 , num4;
           distanceInKm = 100;
           pricePer = 50;
           //getting userinput for the formula.
           userPoint1 = scan.nextInt();
           userPoint2 = scan.nextInt();
           //calculating the userinput with the formula.
           destination = (int)Math.sqrt((userPoint1*userPoint1)+
               (userPoint2*userPoint2));
           //calculation the price + if the kilometer is less then 100 or 0.
           flightPrice =  ((destination/distanceInKm)*pricePer);
           num1= destination%distanceInKm;
           if (num1!=0)
           {
               System.out.println("The price of the flight is:"+" "+
                   (flightPrice+pricePer));
           }
           if (userPoint2==0&&(userPoint1==0)){
               System.out.println("The price of the flight is 0"+ " "+
                   "but we still will check your credit card");}
           //getting credit card number from user.        
           System.out.println("Please enter your credit card number:");
           cerditCard = scan.nextInt();
           //checking if the credit card is valid.
           //1-6 numbers+ first number not 0
           //getting credit car numbers without last one.
           num2 = cerditCard/10;
           // getting last number of credit card
           num3 = cerditCard%10;
           //checking if credit card is valid.
           num4 = num3%7;
           
         
           if (cerditCard<999999&&cerditCard>99999&&(num4==num3))
           {
               System.out.println("bon voyage!");
           }
           else
           {System.out.println("Your credit card is invalid");}
       }
    }// end of program.

     
    Junilu Lacar
    Sheriff
    Posts: 17288
    299
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The names num1, num2, num3 are no better than a, b, c.

    Use names that represent ideas. A name like first6Digits for example is more descriptive than num1. Plus if you name it that you can get rid of a comment because it now becomes redundant.
     
    Junilu Lacar
    Sheriff
    Posts: 17288
    299
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And you are still using the very misleading names destination, userPoint1, and userPoint2.
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please tell us what the changes are. You seem to have the same problems about variable names, e.g. usrPoint1 when you would be better with horizontalDistance, and quite a lot more.
     
    Jenia Levin
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    it can be really nice if you guys can give me an example for a batter names =)
     
    Junilu Lacar
    Sheriff
    Posts: 17288
    299
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jenia Levin wrote:it can be really nice if you guys can give me an example for a batter names =)


    isn’t that exactly what we’ve been doing these last few posts?
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Try: distanceEW distanceNS totalDistance pricePer100Km.
    Some of those would be better as constants:-Other people will doubtless have other suggestions.
     
    Knute Snortum
    Sheriff
    Posts: 7123
    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
    Jenia Levin, please UseCodeTags (that's a link) when you post code.  It is much easier to read the code if you do.
     
    Carey Brown
    Saloon Keeper
    Posts: 9715
    79
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Jenia Levin
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    in this code i have to wride it only in main and to get 6 points of each triangle from user (2 triangles)
    and check the length of each of them and check if they are conguent
    //This program calculate the distance (using pytagoras formula),
    // calculating the price per 100 kilometers. using user input.
    // checking if the credit card is valid(user input)
     
    Bartender
    Posts: 387
    47
    Firefox Browser MySQL Database Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I see you tried to format your code. There will be two tags, the opening tag [code=java] and the closing one [/code]. Can you put your code between the opening and closing code tags, please? That will make it easier to read your code   .
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am not sure I understand the problem. You are asking about congruent triangles, but the code looks the same as for your flight price program from earlier.
    I think it is unfortunate that you have to put all your code into one method, but if you have been told to do that, you will have to comply to get full marks.
    I can't see you getting six points (actually that would be twelve numbers, three xs and three ys for each triangle). I would still use the method I told you about for Pythagoras, and I would write something like this:-Note: you will get the same result if you use x₁ − x₂ and x₂ − x₁.
    Please explain more.
     
    She'll be back. I'm just gonna wait here. With this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic