Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: SIMPLE PROGRAMING PROBLEMS help!!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SIMPLE PROGRAMING PROBLEMS help!!

    here are the problems and try to do them as simple as possible. Thanks you guys are a big help.

    Program 1

    Assume you have N eggs and you pack them 12 to a carton. Write a program that determines how many egg cartons you need and how many eggs will be left over.

    The output should be formatted as follows:

    Total number of eggs is ___
    Number of cartons needed is ___
    Number of eggs left over is ___

    Run your program for N = 11 and N = 37.


    Program 2


    An insurance company will adjust the amount of automobile insurance policies according to the following rules:

    If a customer is under the age of 25 and has had no claims in the past year, there is no increase to their policy price. If they are under the age of 25 and they have had any claims in the past year, their policy price is increased by $100. If they are 25 or older and have had no claims in the past year, there is no increase to their policy price. If they are 25 or older and have had any claims, their policy price is increased by $50.

    Write a program that will determine the amount of policy price increases. Use a variable 'age' for the age and a variable numClaims for the number of claims. Your output should be formatted as follows:

    Age = ___ Claims = ___ Policy Increase = ___
    -----------------------------------------
    any and all responses are helpful!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Please post the code you are having problems with and ask questions about the problems.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    im not having a problem i just wanna see how other people do this

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    I doubt you'll get anyone to do your homework.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    public class Unit5Assignment 
    {
    	public static void main(String [] args)
    	{
    		int Cartons;
    		int N = 37;
    		Cartons = N / 12;
    		int remainder = N % 12;
    		System.out.println("Total number of eggs is " + N);
    		System.out.println("Number of cartons needed is " + Cartons);
    		System.out.println("Number of eggs left over is " + remainder);
    	}
    }

    // My code for problem 1
    Last edited by Norm; October 7th, 2012 at 08:58 PM. Reason: Added code tags

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    im struggling and want help thats all

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    When you post code, please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Please explain what your problems are.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Im struggling with the 2nd problem. i get i would use logical operators but im not sure if i can use switch statment with logical operators.

    also i learned about bitwise operators, is there any way i can incorperate them. thanks

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    use switch statment with logical operators.
    There is no connection between a switch statement and logical operators.
    You would use logical operators with an if statement.

    The normal order of things when writing a program is to figure out the logic for the program and after that is done, see what coding techniques to use to implement the logic. You seem to be worrying about coding techniques before working out the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    silverpen10 (October 8th, 2012)

  11. #10
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    your right im thinking to much about code before i even get there thank you

  12. #11
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    import java.util.Scanner;
     
    public class Unit5Assignment2
    {
    	public static void main(String [] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("What is your age?");
    		int Age = input.nextInt();
     
    		System.out.println("How many insurence claim have you had in the past year?");
    		int numClaims = input.nextInt();
     
    		if (Age < 25 & numClaims == 0);
     
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $0");
     
    		if (Age < 25 & numClaims >= 1);
     
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $100");
     
    		if (Age >= 25 & numClaims == 0);
     
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $0");
     
    		if (Age >= 25 & numClaims >= 0);
     
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $50");
    	}
    }

    the output is displaying all "if" statments intsted of picking 1 plz help

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Please post the contents of the console from when you execute the program showing what you type in and what the program prints out.

    You need to compile the code with the -Xlint option. The compiler will give a warning telling you what the problem is: empty statement after if

    You should ALWAYS use {}s with if, for and while statements :
      if(var > 4 && another < 3) {
       //  do this if true
      }
    Not using them is legal, but you will have problems sometime when you add another statement and forget to add the {}s. Add them immediately when first typing in the if, for and while


    Another possible problem is the bitwise & operator. && is the AND operator. See:
    Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Oct 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    I don't get what the problem with the egg carton task is. It looks like it should work fine.

    As for your string of If statements, it looks like you're using a bad "and" operator. Java's logical and is &&, and I see you're using a single &. I'd have to look, but I'm thinking this might have another meaning when used in singular.

    Also, I'd be careful with that scanner. It'll start throwing exceptions if you mistype and enter text when it's asking you for a number. I should know - I'm a pretty sloppy typist.

  15. #14
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Here's my new program, it's very similar just changed & to && and put brackets
    import java.util.Scanner;
     
    public class Unit5Assignment2
    {
    	public static void main(String [] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("What is your age?");
    		int Age = input.nextInt();
     
    		System.out.println("How many insurence claim have you had in the past year?");
    		int numClaims = input.nextInt();
     
    		if (Age < 25 && numClaims == 0);
    		{
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $0");
    		}
    		if (Age < 25 && numClaims >= 1);
    		{
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $100");
    		}
    		if (Age >= 25 && numClaims == 0);
    		{
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $0");
    		}
    		if (Age >= 25 && numClaims >= 0);
    		{
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $50");
    		}
    	}
    }

    heres the output
    Unit 5 Assignment 2 prob.jpg
    maybe i should just take out the input scanner
    Last edited by silverpen10; October 9th, 2012 at 02:28 PM.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Did you add the -Xlint option for the javac program so you would get the warning I told you about?
    Here is a sample:
    Running: D:\Java\jdk1.6.0_29\bin\javac.exe -Xlint  Unit5Assignment2.java
     
    Unit5Assignment2.java:19: warning: [empty] empty statement after if
    		if (Age < 25 && numClaims == 0);
    		                               ^

    The ; ends the if statement. Anything following the ; is NOT part of the if statement.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    it doesnt matter if i take out the scanner i get the same output

  18. #17
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    im sorry i don't understand do i add that in my program files?

  19. #18
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    nvm i got it now how do i fix that warning/error.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    javac is an OS command that compiles the source. Commands can take commandline options to control how they work and what they do. -Xlint is a commandline option for the javac command.
    The first line (begins with Running: ) of the included text in my post shows the commandline I used to compile your program. See: javac - Java programming language compiler
    If you don't understand my answer, don't ignore it, ask a question.

  21. #20
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    if (Age < 25 && numClaims == 0){
    			System.out.println("Age = " + Age + "  Claims = " + numClaims + "  Policy Increase = $0");
    		}
    It worked thank you very much

  22. #21
    Member
    Join Date
    Oct 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Ah, yes, the semicolon after the if statement. Can't believe I didn't spot that. Yes, if you put a semicolon after an if statement, you end it there and the following code block executes because it's not attached to the preceding if.

  23. #22
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Just because you mentioned it, I thought I would point something out about bitwise operators. It doesn't do logic that normally makes any sense whatsoever. It does the low level calculations that just happen to be called the same thing and modeled off of the same thing as boolean logic. Bitwise operators are usually only used for hashing data, or multiple data into a single variable, letting you do things like having 2 4-bit numbers in a byte or something like that.

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    The & is also a logical operator(otherwise the compiler would give an error message.
    The && operator is a short-circuit operator. It stops evaluation when the results are known. See the following. First and last will execute the ++x. The middle one doesn't
     
          int x = 0;
          boolean b1 = (false & ++x >= 0);    // & operator does both tests
          System.out.println("b1="+b1 + ", x=" + x);  // x=1
          boolean b2 = (false && ++x >= 0);              //<<<  && operator ends tests
          System.out.println("b2="+b2 + ", x=" + x);  // x=1   unchanged
          boolean b3 = (false & ++x >= 0);
          System.out.println("b3="+b3 + ", x=" + x);  // x=2
    If you don't understand my answer, don't ignore it, ask a question.

  25. #24
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    & is not a logical operator, strictly. However it is good to know that it is a short-circuit work around, and not a 8-times the calculation minimum faceplant.

  26. #25
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: SIMPLE PROGRAMING PROBLEMS help!!

    Quote Originally Posted by lightOfDay View Post
    & is not a logical operator, strictly.
    Well, actually, and strictly, it is when both operands are boolean. This according to the Java Language Specification. (Paragraph 15.22.2 in version 3 of the jls.) Same for '^' and '|'

    15.22.2
    Boolean Logical Operators &, ^, and |
    When both operands of a &, ^, or | operator are of type boolean or Boolean, then
    the type of the bitwise operator expression is boolean. In all cases, the operands
    are subject to unboxing conversion (§5.1.8) as necessary.

    For &, the result value is true if both operand values are true; otherwise, the
    result is false.
    For ^, the result value is true if the operand values are different; otherwise,
    the result is false.
    For |, the result value is false if both operand values are false; otherwise,
    the result is true.
    So, it is not just some squirrelly little thing that "Just Happened." The behavior has been deterministically specified by the author and maintainers of the language.


    Cheers!

    Z
    Last edited by Zaphod_b; October 16th, 2012 at 05:11 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problems with GUI, seems simple fix but I can't figure it out
    By Harrald in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2012, 04:51 AM
  2. Java programing help plz!
    By qazwsx in forum Java Theory & Questions
    Replies: 1
    Last Post: July 21st, 2012, 07:01 PM
  3. Programing
    By goha14 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2012, 04:44 PM
  4. help with a programing assignment.
    By oscar_m in forum Object Oriented Programming
    Replies: 4
    Last Post: May 1st, 2011, 02:00 PM
  5. Im having problems compiling a very simple .js file to .exe
    By idesyl in forum Member Introductions
    Replies: 3
    Last Post: March 10th, 2011, 05:16 AM