Click here to Skip to main content
15,077,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
These are what I need to do. I'm a 2nd year IT student and I'm struggling so much please help me. The codes below worked but I want to apply inheritance (with scanner) in my program, I can't do it.

- The program should ask an integer f rom the user. This should be perf ormed by the main method.
- Construct three (3) methods named showNumberPlus10(), showNumberPlus100(), and
showNumberPlus1000(). Each method should perf orm the task its name implies.
- Call the methods af ter the user input to display three (3) outputs in separate lines.

The output should look like this:

Enter an integer: 20
20 plus 10 is 30.
20 plus 100 is 120.
20 plus 1000 is 1020.

What I have tried:

import java.util.Scanner;

class LabExer1B {
	public static void main(String []args) {
		Scanner sc = new Scanner(;
		int num = 0;
	System.out.print("Enter an integer: ");
		num = sc.nextInt();
	static void showNumberPlus10(int num) {
		System.out.println(num + " plus 10 is " + (num + 10) + ".");
	static void showNumberPlus100(int num) {
		System.out.println(num + " plus 100 is " + (num + 100) + ".");
	static void showNumberPlus1000(int num) {
		System.out.println(num + " plus 1000 is " + (num + 1000) + ".");
Updated 22-Sep-21 23:00pm

I think you need to go back to your actual assignment and read it very carefully - there is nothing in the code you show which is suitable for inheritance in any form. In order to use inheritance, you need to have a base class which does useful work, and a second class which extends that to do more useful work or more specific work.

Think about a Car class: you don't buy a Car, you buy a Ford, or a Mercedes, or a Fiat - all of which are Cars - they inherit from the Car class and extend it to something that can be built and sold to you. But you learn to drive a Car, and apply that knowledge unchanged regardless of the manufacturer.
Car is a base class, Ford is an derived class with inherits from Car.
(And yes, in the real world you would have Fiesta class, AClass class, and individual model variants within those which you would actually buy - you wouldn't buy "a Ford" for the same reasons you wouldn't buy "a Car" ... but lets not stretch a metaphor too far, eh?)

So to use inheritance in that example just isn't feasible: there is nothing there that would be a suitable base class, or that would benefit from inheritance.
skwoals 23-Sep-21 3:14am
Thank you. So I don't need to use inheritance for the program? Why did my prof send us this code for reference? I don't know how to do it with scanner..

class sample {

int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);

public class example extends sample {
public static void main(String []args) {
int a = 20, b = 10;
example demo = new example();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
I don't know what they taught you in year one but inheritance should have been a fundamental part of it. You can find a clear description at Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)[^]
skwoals 23-Sep-21 6:11am
Nope they didn't teach inheritance when I was in 1st year. Our topic now is Object-Oriented Programming and he discussed that example^ of program using inheritance. I'm so confused because he didn't teach us how to use it with scanner so I have no idea how to put scanner and inheritance in the same program and how to make it work.
Richard MacCutchan 23-Sep-21 6:41am
The Scanner class has nothing to do with inheritance, it is used to get input data from the console. As for inheritance, if your professor has not explained it to you, then you need to go and have a chat with him. There is no way we can teach it in a Quick Answers forum.
skwoals 23-Sep-21 12:27pm
I see. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900