Number of pages =========== rate per page (cents)
First 100 ============ 3.0
Next 100 ============= 2.5
Above 200 ============ 2.0

Example : Printing cost for 200 pages is $5.5
Printing cost for 301 pages is $7.5

write a java(application) program to do the following:
- prompt the user for the number of pages printed
-calculate the cost of printing
-display the input entered and the printing cost in dollars with appropriate description

I don't see a question. Please try first and then ask a real question. No one is going to do your homework for you.

How would you solve this by hand? One way would be like this:

450 pages

first 100 pages: $3
leaves 350 pages
second 100 pages: $2.50
Total is now $5.50, 250 pages left to deal with.

third 100 pages: $2.00
Total is now $7.50, 150 pages left to deal with.
fourth 100 pages: $2.00
Total is now $9.50, 50 pages left to deal with.
fifth 100 pages (or fraction thereof): $2.00
Total is now $11.50, 0 pages left to deal with.


So your first two steps are unique and then you can enter a loop - does that help?

import java.util.*;

public class Homework2 {

public static void main(String[] args){

double pages, total;
Scanner input = new Scanner(System.in);

System.out.println("Enter the number of pages");

pages = input.nextDouble();

correct to start with this ?

pages = input.nextDouble();

Number of pages is usually a whole number.
Can your app have 4.2 pages?

that means it should be Int pages, total; instead of double?

Yes, use ints. You can use double for the price, if you like, but there are good reasons to use integers or a Money class to represent money, so $2.50 would be 250 cents. Don't worry about that now, though, doubles will be fine for your purposes.

For your next step, you might want to figure out if the total is $0, $3, or more than $3.

System.out.println("Enter the number of pages");
pages = input.nextInt();
total= total / 100;


System.out.println("Your total is:");
if (pages < 100){
total = (pages * .03);
}
else

if ( (pages > 100) && (pages < 200) ){ 
total = (pages - 100) * .025 + (100 * .03);
}
else

if (pages > 200){
total = (pages - 200) * .02 + (100 * .03) + (100 * .025);
}
 

}
}

Okay, now you need to start asking questions. Is this working for you? If not, why not? What is it doing that's wrong? What are you seeing, and what do you expect to see? Asking this question is the first step towards answering it for yourself.

Just posting code means you want us to just fix it for you - that's not what's going to happen.

What happens when you compile and execute it?
If there are errors, please copy and paste the full text here.

i try to compile it , but it can't solve it. i don't know why. is there something missing ?

import java.util.*;

public class Homework1 {

public static void main(String[] args){

double pages,total;
Scanner input = new Scanner(System.in);

System.out.println("Enter the number of pages");

pages = input.nextInt();

 

if (pages < 100){
total = (pages * .03) + 5;
}
else

if ( (pages > 100) && (pages < 200) ){ 
total = (pages - 200) * .025 + 15;
}
else

if (pages > 200){
total = (pages - 200) * .02 + 40;

 
System.out.println(total);

System.out.println("Your total is:");
}
}
}

OUTPUT :

Enter the number of pages
90
Your total is:

----jGRASP: operation complete.

----jGRASP exec: java Homework1

Indent your code properly, then look at when
System.out.println("Your total is:");
will be executed.

Indent your code properly, then look at when
System.out.println("Your total is:");
will be executed.

sorry. i don't get what you meant.

You are not getting any output at the end. That's because of a logic error in your code. You will find it a lot easier to locate that error if you indent your code properly so you can see which statements are part of which blocks.

By "indent properly", we're talking about lining up your statement blocks so you can see how they relate.

There are a few ways to do this, I think the easiest to read is what's called "Allman style". Looks like this:

public class SomeClass
{
  int a;
 
  public void someMethod(int b) 
  {
    a = b;
    if ( someCondition )
    {
      doSomething();
    }
    else if (secondCondition)
    {
      doSomethingElse(); 
    }
    else 
    {
      doNothingAtAll();
    }
  }
}

By lining up the opening and closing braces, you make the overall structure obvious, this helps you to spot errors, like the one that's bugging you right now.

import java.util.*;
 
public class Homework1 {
 
	public static void main(String[] args)
	{
 
		double pages,total;
		Scanner input = new Scanner(System.in);
 
		System.out.println("Enter the number of pages");
 
		pages = input.nextInt();
 
 
 
		if (pages < 100)
		{
			total = (pages * .03) + 5;
		}
		else if ( (pages > 100) && (pages < 200) )
		{ 
			total = (pages - 200) * .025 + 15;
		}
		else if (pages > 200)
		{
			total = (pages - 200) * .02 + 40;
 
 
			System.out.println(total);
 
			System.out.println("Your total is:");
		}
	}
}

I have indented your code for you. There is a logic error in your program. I hope you will be able to see what's wrong after I have indented it properly :)
While you can write code in any way you like, it will be helpful to stick to a 'universal' set of conventions of code-writing by programmers so that others can help you look through.

In addition... following the code that you pasted, it shouldn't even print out "total number of page:".

Offtopic: time for me to give back the community during the summer hols! :D

Member Avatar for ztini

Ack! Silly hanging brackets...lets fix that:

import java.util.Scanner;


public class Homework1 {
 
	public static void main(String[] args) {
 
		double pages, total;
		Scanner input = new Scanner(System.in);
 
		System.out.println("Enter the number of pages");
 
		pages = input.nextInt();
 
		if (pages < 100) {
			total = (pages * .03) + 5;
		}
		else if ((pages > 100) && (pages < 200)) { 
			total = (pages - 200) * .025 + 15;
		}
		else if (pages > 200) {
			total = (pages - 200) * .02 + 40;
 
			System.out.println(total);
			System.out.println("Your total is:");
		}
	}
}

Also, you don't necessarily need brackets if your if-statement is only one command. So you could collapse the above like this:

import java.util.Scanner;


public class Homework1 {
 
	public static void main(String[] args) {
 
		double pages, total;
		Scanner input = new Scanner(System.in);
 
		System.out.println("Enter the number of pages");
 
		pages = input.nextInt();
 
		if (pages < 100)
			total = (pages * .03) + 5;
		else if ((pages > 100) && (pages < 200)) 
			total = (pages - 200) * .025 + 15;
		else if (pages > 200) {
			total = (pages - 200) * .02 + 40;
 
			System.out.println(total);
			System.out.println("Your total is:");
		}
	}
}

Also, you'll want to choose your package imports wisely. Just doing:

import java.util.*;

does work, but you'll want to define the classes specifically that you are importing; not just the package.

import java.util.Scanner;

The importance of this will become evident when you start overriding classes in packages later.

commented: Way to go... let him do his own work! -2
Member Avatar for ztini

I dare you to turn this in:

import java.text.DecimalFormat;
import java.util.Scanner;


public class PrintCalculator {
 
	public static double getCost(int pages) {
		return pages > 200 ? pages * 2.0 : pages > 100 ? 100 * 3.0 + (pages - 100) * 2.5 : pages * 3.0;
	}
	
	public static void main(String[] args) {
		System.out.print("Pages printed:  ");
		System.out.println("Printing Cost: " + DecimalFormat.getCurrencyInstance()
				.format(getCost(new Scanner(System.in).nextInt())));
	}
}

you don't necessarily need brackets if your if-statement is only one command. So you could collapse the above like this:

Never!
This will cause you grief. Then extra key strokes are well worth the time. When you forget the brackets and think a second statement is in the scope you'll spend hours trying to figure it out. Always put brackets around the code.

commented: Testify, son! +11
Member Avatar for ztini

Never!
This will cause you grief. Then extra key strokes are well worth the time. When you forget the brackets and think a second statement is in the scope you'll spend hours trying to figure it out. Always put brackets around the code.

If that kind of error causes you to "spend hours trying to figure it out", your issue lies not with brackets, but with an inability using a debugger.

To each his own. I never clutter my code up with useless characters and I never have bracket issues. Really, it comes down to preference. A good IDE helps too.

I never clutter my code up with useless characters

i guess that includes commenting your code also. I don't see ANY comments in the code you posted

Also, you don't necessarily need brackets if your if-statement is only one command.

The compiler doesn't need them, but you should always put them in, especially if you're having trouble with dangling elsen.
And if you're having bracketing troubles, use Allman-style, not K&R. K&R is great if you're publishing on dead trees and want to save vertical space, but that's not really an issue here.

Member Avatar for ztini

// This is where I prove you are an idiot

Why would I document code on a forum? That is asinine, lol.

// logic complete

Why would I document code on a forum? That is asinine, lol.

One of the ideas on the forum is to teach students programming.
Good commenting style is not taught in schools.

Not all beginning students will understand posted code. Comments can help.

Do you consider good comments in a program important?

Member Avatar for ztini

The compiler doesn't need them, but you should always put them in, especially if you're having trouble with dangling elsen.
And if you're having bracketing troubles, use Allman-style, not K&R. K&R is great if you're publishing on dead trees and want to save vertical space, but that's not really an issue here.

Like I said, its a preference. I prefer seeing the code condensed down a bit to get a better overall feel of it.

Member Avatar for ztini

One of the ideas on the forum is to teach students programming.
Good commenting style is not taught in schools.

Not all beginning students will understand posted code. Comments can help.

Do you consider good comments in a program important?

Only API comment blocks are important. Tell me what the method/constructor takes, what I get, and what it does. Cluttering up your code with a million // is retarded as can be. If you have logically abstracted your code well and used well defined design patterns, you should only need one solid api comment block before a method. If your code is so difficult to read that you NEED to supplement each line with //, you need to take a look at your approach and try again.

If that kind of error causes you to "spend hours trying to figure it out", your issue lies not with brackets, but with an inability using a debugger.
To each his own. I never clutter my code up with useless characters and I never have bracket issues. Really, it comes down to preference. A good IDE helps too.

Funny, I don't use an IDE at all and I've never needed a debugger for Java... The first time I had this sort of problem it probably did take me an hour or two to sort it out. That was good, it meant I can spot these things now without mechanical assistance.

Only API comment blocks are important. Tell me what the method/constructor takes, what I get, and what it does. Cluttering up your code with a million // is retarded as can be. If you have logically abstracted your code well and used well defined design patterns, you should only need one solid api comment block before a method. If your code is so difficult to read that you NEED to supplement each line with //, you need to take a look at your approach and try again.

If you're stepping someone through a well-written piece of code, it's probably good to explain what you're doing, especially if you're relying on logically abstracted (==less explicit) code and design patterns (generally non-intuitive, which is why you have to learn them).

Yes, production code should not require line-by-line commenting. Sample code should always be commented, either in-line or in following text.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.