• Register
1 vote
46 views

Problem:

I just started to learn java programming and I am very noob a it. I wrote a code which shows me error

Main.java:1: error: class, interface, or enum expected
{ 
^
Main.java:3: error: class, interface, or enum expected
    public String getName()  
           ^
Main.java:6: error: class, interface, or enum expected
    } 
    ^
Main.java:9: error: class, interface, or enum expected
    public boolean isNull()  
           ^
Main.java:12: error: class, interface, or enum expected
    } 
    ^
5 errors

The code I wrote is:

{ 
   
    public String getName()  
    { 
        return "Not Available"; 
    } 
  
   
    public boolean isNull()  
    { 
        return true; 
    } 
} 


   public class main {
        
     public static void main (String [] args){
       
       system.out.println(emp1.getname());

}

}

Please help me out to solve this problem.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

A null object refers to an object without any reference or an object defined with neutral/null functionality/behavior. These null objects need to be checked to ensure that they are not null while accessing any member or invoking any methods

abstract class Emp 
{ 
    protected String name; 
    public abstract boolean isNull(); 
    public abstract String getName(); 
}

class NoClient extends Emp 
{ 
    @Override
    public String getName()  
    { 
        return "Not Available"; 
    } 
  
    @Override
    public boolean isNull()  
    { 
        return true; 
    } 
}

class EmpData {
public static final String [] names = {"fff"}
public static Emp getClint(String name)

{ 
        for (int i = 0; i < names.length; i++)  
        { 
            if (names[i].equalsIgnoreCase(name)) 
            { 
                return new Coder(name); 
            } 
        } 
        return new NoClient(); 
    } 
} 

}

public class Main {

     public static void main(String[] args)
{
     Emp emp1 = EmpData.getClient("fff"); 
     Emp emp2 = EmpData.getClient("xxx");

     System.out.println(emp1.getName()); 
      System.out.println(emp2.getName());       

}

}

Hope you understad this.

Happy coding. 

11 6 4
34,950 points

Related questions

1 vote
1 answer 66 views
66 views
Problem: For the following program of java: public static void main(String[] args) { int A; char a = A; if (Character.isUpperCase()) { System.out.println a + " is an uppercase character."; } } After compiling this code it shows the message: javac -classpath .:/ ... isUpperCase()) { ^ Main.java:6: error: class, interface, or enum expected } ^ 4 errors How to solve this one? Thanks in advance.
asked Apr 27, 2020 samhaz 5k points
0 votes
1 answer 10 views
10 views
Problem: The class interface or enum expected error is a compile-time error in Java which arises due to curly braces. Typically, this error occurs when there is an additional curly brace at the end of the program.
asked Feb 19 Ali 4.4k points
0 votes
1 answer 15 views
15 views
Thanks for all the help
asked Feb 6 TeamScript 13.5k points
1 vote
1 answer 35 views
35 views
Problem: I have started learning java programming a few days ago and I doing practice. I wrote a code which throws me some error. The code I wrote is: Scanner scnr = new Scanner(System.in); String userInput = " "; boolean inputDone = false; while (!inputDone) { System. ... : error: class, interface, or enum expected } ^ 11 errors It is very confusing to me. Please help me out to solve this thing.
asked Apr 6, 2020 LizzyM 6.1k points
1 vote
1 answer 198 views
198 views
Problem: &ldquo;This method must return a result of type boolean&rdquo; What does it mean? I have a sample java program but I can&rsquo;t run my program due to the above error. Once I try to run the program it gives back the error. Please find my code snippet below to understand my problem: ... && board[n] == board[n+1] && board[n] == board[n+2]) { return true;} } } Thanks for your valuable time
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 18 views
18 views
I want a proper solution to remove this warning on sonar.
asked Jan 19 TeamScript 13.5k points
0 votes
1 answer 40 views
40 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 5.8k points
0 votes
1 answer 27 views
27 views
Hi, when i try to execute this code i always get the error message from above, and i really want to know why, already tried to put a &ldquo;public class XXXX&rdquo; before the code, but not work anyway. public static String invertirPalabras(String cadena) { cadena += " "; String ... + palabra; if (cadena.charAt(i) == ' ') { nuevaCadena += palabra; palabra = ""; } } return nuevaCadena.trim(); }
asked Aug 2, 2020 Daniel Aguilar
3 votes
1 answer 1.6K views
1.6K views
Problem: Hello Kodlogs, I am a novice Java programmer. I just wrote my very first program a few minutes earlier. Now I want to write a program that can add two different integer numbers for me. I guess I done my coding well but it says something wrong with my writing and throws back an error: &ldquo ... []args){ int x = 10; int y = 15; int sum = x+y; System.out.println("Sum is: " + sum); } }
asked Mar 23, 2020 Gavin 15.3k points
1 vote
1 answer 99 views
99 views
Problem: I am a begginer in java programming language and I have been coding for just some days. To practice my programming I wrote this code in java: public class Main { public static void main(String[] argv){ int 2count, h-l, a/b, } } But It shows me the error message ... , . expected Syntax error on token ",", ;expected at Main.main(Main.java:3) Whats is wrong in my code can anyone help me out?
asked Apr 6, 2020 LizzyM 6.1k points