I am learning inheritance and while doing so on Eclipse, I get an error when trying to run the following program:
import java.io.*;
import java.util.*;
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
The error that I get is "Error: Could not find or load main class superex$A"; What is causing this error, and how do I fix it?
As you start with java the best thing to do is to create 2 files singinh.java and singinh1.java, move the related code into the corresponding file and launch your java command using singinh1 as main class.
In singinh.java you will have:
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
In singinh1.java you will have:
import java.io.*;
import java.util.*;
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
Then you will be able to launch singinh1
In java you cannot have more than one public class in the same source file. Also the name of the source file should be the name of the public class in that source file exactly.
Since your main method is in "singinh1" class , keep it as the public class and remove public keyword from "singinh" class. Name the source file name to
singinh1.java .
Modified code :
import java.io.*;
import java.util.*;
class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
Related
This is the program for print the character of given index.But is gets error in function.
I give the function return type as char.But compiler told to rename the return type of main function
import java.util.Scanner;
class Demo5{
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
public char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
}
}
public static void main(String[] args)
{
Scanner n=new Scanner(System.in);
String p=n.next();
int q=n.nextInt();
System.out.println(showchar(p,q));
}
public static char showchar(String s,int num)
{
char c=s.charAt(num);
return c;
}
I recived some tasks as homework after I did just 1h of course about interfaces, did them but this gave me real problems:
An interface I1 having: method: int read();
A class A implements the interface and containing: the method int read() that read a value from the keyboard;
A class B containing:a variable c type I1, a constructor with one parameter for assigning value for c, a method afis() that display the summ of the numbers.
This is what I did so far, but I'm stuck here, how can i assign a value to c and add it with the n1 from A class?
package toDo2;
import javax.swing.JOptionPane;
public interface i1 {
int read();
}
class A implements i1 {
public int read() {
int n1 = Integer.parseInt(JOptionPane.showInputDialog("Introduce the value for n1"));
return n1;
}
}
class B {
int sum;
i1 c;
B(i1 c) {
}
}
class toDo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
B n1 = new B(/*here must be the value of c which is i1 type*/);
}
}
It appears, you need to create an instance of class A in your main method because it is so far the only class implementing interface i1:
class B {
int sum;
i1 c;
B(i1 c) {
this.c = c;
}
}
class toDo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
B n1 = new B(new A());
// ...
}
}
Also, class B won't be aware of specific implementation of its c field - it will be able to invoke method int read() defined in your interface.
This will do
B n1 = new B(new A());
Basically your class A is of type I1 because it's implementing I1.
Here you go:
import java.util.Scanner;
public class Main{
public static void main(String []args){
I1 a = new A();
a.read(); //this call will initiate taking input
B n1 = new B(a);
int sum = n1.sum(a.getFirstNumber(), a.getSecondNumber());
System.out.println(sum);
}
}
interface I1 {
void read();
int getFirstNumber();
int getSecondNumber();
}
class A implements I1 {
int n1;
int n2;
public void read() {
Scanner in = new Scanner(System.in);
n1 = in.nextInt();
n2 = in.nextInt();
}
public int getFirstNumber() {
return n1;
}
public int getSecondNumber() {
return n2;
}
}
class B {
I1 c;
B(I1 c) {
this.c = c;
}
public int sum(int n1, int n2) {
return n1+n2;
}
}
I'm trying to build a library with netbeans, I'm building so:
New project...Java...Java application:
package somma;
public class Somma {
public static int somma(int a, int b) {
int s = a + b;
return s;
}
}
With this main
package somma;
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 2;
int s = Somma.somma(a, b);
System.out.println(s);
}
}
After that, right click on the project...properties...Buid...Packaging....click on the project again and clean and build. Now I created a Somma.jar, for to try the new library I build an exmple project:
package uselibrary;
import static somma.Somma.somma;
public class UseLibrary {
public static void main(String[] args) {
int a = 1;
int b = 2;
int s = somma(a, b);
System.out.println(s);
}
}
Run correctly, but there is a problem, when I import the library, I would like to avoid this name import static somma.Somma.somma; I would like to change with this name import somma;
How can I do that ?
Okay try this edit
package somma;
public class Somma {
public int somma(int a, int b) {
int s = a + b;
return s;
}
}
And
package uselibrary;
import somma.Somma;
public class useLibrary {
public static void main(String[] args) {
Somma som = new Somma();
int a = 1;
int b = 2;
int s = som.somma(a, b);
System.out.println(s);
}
}
How can I invoke a method and print its result?
public class Test {
public static int main (String args[]){
System.out.println(total);
}
public int numbers (int a, int b){
int total;
total = a + b;
return = total;
}
}
Try this instead:
public class Test {
public static void main (String args[]){
System.out.println(numbers(1, 2));
}
public static int numbers(int a, int b){
int total;
total = a + b;
return total;
}
}
Variables are scoped to the method or class in which they are defined, therefore the 'total' variable is accessible only in the 'numbers' method
public class Test {
public static void main (String args[]){
System.out.println(numbers(anumber,bnumber));
}
public static int numbers (int a, int b){
int total;
total = a + b;
return total;
}
I'm trying to write a simple code in Java but I keep getting error for the method calling.
package tutorialproject2;
import java.util.Scanner;
public class Tutorialproject2 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
InputTest();
Calculate();
}
public static void InputTest(){
String message = input.nextLine();
System.out.println(Hello(message));
}
public static String Hello(String message){
if (message.equals("Hi")){
return "Hello";
}else{
return "Goodbye";
}
}
public int Calculate(int a,int b){
a = input.nextInt();
b = input.nextInt();
int answer = a * b;
return answer;
}
You have the method Calculate(int a,int b) with 2 parameters but call the method without parameters Calculate().
I suppose you should change the method Calculate(int a,int b) to
public static int Calculate(){
int a = input.nextInt();
int b = input.nextInt();
int answer = a * b;
return answer;
}
and as #Visme mentioned, to add static keyword.
or you can leave your method as
public int Calculate(){
int a = input.nextInt();
int b = input.nextInt();
int answer = a * b;
return answer;
}
In this case in the main function, you should call the method this way:
new Tutorialproject2().Calculate();
Non static method calculate is called from static function (main)
should be in this way, int var = Calculate(3,5); , can't be calculate() alone as it has a return type and arguments.
You could call methods with return type void alone like InputTest();
as you return integer from this method and the method has parameters so you should pass the required parameters here (of integer type),
public static int Calculate(int a,int b){
. . .
return answer;
}