# SIMPLE PROGRAMING PROBLEMS help!!

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• October 7th, 2012, 05:28 PM
silverpen10
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!!
• October 7th, 2012, 05:41 PM
Norm
Re: SIMPLE PROGRAMING PROBLEMS help!!
• October 7th, 2012, 08:15 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
im not having a problem i just wanna see how other people do this
• October 7th, 2012, 08:36 PM
Norm
Re: SIMPLE PROGRAMING PROBLEMS help!!
I doubt you'll get anyone to do your homework.
• October 7th, 2012, 08:54 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
Code :

```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
• October 7th, 2012, 08:55 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
im struggling and want help thats all
• October 7th, 2012, 08:56 PM
Norm
Re: SIMPLE PROGRAMING PROBLEMS help!!
[code=java]
[/code]
to get highlighting and preserve formatting.

• October 8th, 2012, 11:34 AM
silverpen10
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
• October 8th, 2012, 11:38 AM
Norm
Re: SIMPLE PROGRAMING PROBLEMS help!!
Quote:

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.
• October 8th, 2012, 06:20 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
your right im thinking to much about code before i even get there thank you
• October 8th, 2012, 09:14 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
Code java:

```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
• October 8th, 2012, 09:55 PM
Norm
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 :
Code :

``` 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.
• October 9th, 2012, 10:04 AM
Fazan
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.
• October 9th, 2012, 02:15 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
Here's my new program, it's very similar just changed & to && and put brackets
Code java:

```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
Attachment 1460
maybe i should just take out the input scanner
• October 9th, 2012, 02:20 PM
Norm
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:
Code :

```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.
• October 9th, 2012, 02:20 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
it doesnt matter if i take out the scanner i get the same output
• October 9th, 2012, 02:23 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
im sorry i don't understand do i add that in my program files?
• October 9th, 2012, 02:26 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
nvm i got it now how do i fix that warning/error.
• October 9th, 2012, 02:31 PM
Norm
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
• October 9th, 2012, 02:31 PM
silverpen10
Re: SIMPLE PROGRAMING PROBLEMS help!!
Code java:

```if (Age < 25 && numClaims == 0){ System.out.println("Age = " + Age + " Claims = " + numClaims + " Policy Increase = \$0"); }```
It worked thank you very much
• October 9th, 2012, 03:56 PM
Fazan
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.
• October 13th, 2012, 10:12 AM
lightOfDay
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.
• October 13th, 2012, 11:25 AM
Norm
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
Code :

```  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```
• October 16th, 2012, 02:32 PM
lightOfDay
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. :D
• October 16th, 2012, 05:06 PM
Zaphod_b
Re: SIMPLE PROGRAMING PROBLEMS help!!
Quote:

Originally Posted by lightOfDay
& 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 '|'
Quote:

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
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last