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

# Thread: SIMPLE PROGRAMING PROBLEMS help!!

1. ## 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!!

3. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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

4. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

I doubt you'll get anyone to do your homework.

5. ## 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

6. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

im struggling and want help thats all

7. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

[code=java]
[/code]
to get highlighting and preserve formatting.

8. ## 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. ## 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.

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

silverpen10 (October 8th, 2012)

11. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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

12. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

```import java.util.Scanner;

public class Unit5Assignment2
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);

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. ## 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.

14. ## 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. ## 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);

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

16. ## 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.

17. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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

18. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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

19. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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

20. ## 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

21. ## 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. ## 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. ## 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. ## 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```

25. ## 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. ## Re: SIMPLE PROGRAMING PROBLEMS help!!

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 '|'

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

Page 1 of 2 12 Last