Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed what is wrong in my code?

+2 votes
asked Sep 23, 2021 by Onyx 443 (170 points)
closed Oct 25, 2021 by Admin
import java.util.Scanner;
public class Main
 String userName;

String password;

 
public static void
main (String[]args)
{
  
this.userName = aUserName;
  
this.password = aPassword;

}
public String getUserName ()
{
  
return userName;

}

public void
setUserName (String aUserName)
{
  
this.userName = aUserName;

}
public String getPassword ()
{
  
return password;

}

public void
setPassword (String aPassword)
{
  
this.password = aPassword;

}
 

{
  
int num = 18;
  
if (num % 2 == 0)
    {
      
System.out.println ("you are qualified to vote");
    
}
  
  else
    
    {
      
System.out.println ("you are not qualified to vote");
    
}

}

}
closed with the note: answered

1 Answer

+1 vote
answered Sep 23, 2021 by Peter Minarik (63,320 points)
edited Sep 23, 2021 by Peter Minarik
 
Best answer

First of all, you need an opening brace after your class declaration to indicate where the body of your class starts:

public class Main
{

You cannot use the this pointer in a static method, such as the main();

Your last function has only a body, but no signature to it. Give it a return value, a name, and a parameter list. E.g.:

public void PrintEligibility()
{
    int num = 18;
    if (num % 2 == 0)
    {
        System.out.println ("you are qualified to vote");
    }
    else
    {
        System.out.println ("you are not qualified to vote");
    }
}

Next thing to do is instantiating the class and calling your functions on it so your program actually does something as right now (if the invalid statements are removed from the main()) it does nothing.

Your code refactored, fixed and some logic changed to better match real life would look like this:

User.java

public class User
{
    private String _name;
    private String _password;
    
    public User(String name, String password)
    {
        _name = name;
        _password = password;
    }
    
    public String GetName() { return _name; }
    public void SetName(String name) { _name = name; }
    
    public String GetPassword() { return _password; }
    public void SetPassword(String password) { _password = password; }
    
    public boolean CanVote(int age) { return age >= 18; }
}

Main.java

import java.util.Scanner;

public class Main
{
    public static void main (String[] args)
    {
        User user = new User("Thomas Tester", "Easy password");
        if (user.CanVote(18))
            System.out.println("You are qualified to vote.");
        else
            System.out.println("You are not qualified to vote.");
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...