Login From Program in java

In this post, we will learn Login From Program in java Programming language.

Question:

Write a program to check whether the user is an authorized user or not. Create a class Login with the private attributes userName and password,write a method public boolean validate() in the Login class, this method should check whether the given userName is john and the password is 123abc in that case return true else return false. Print “Valid user” or “Invalid user” based on the value returned from validate method. 
Note: Write a constructor in the Login to set userName and password. Include appropriate getter method

Sample Input 1:
Enter the username:

john
Enter the password:

123abc

Sample Output 1:
Valid user

CODE:

import java.util.*;
public class Login
{
    private String userName,password;
    public Login(String userName,String password)
    {
        this.userName=userName;
        this.password=password;
    }
    public String getUserName()
    {
        return this.userName;
    }
    public String getPassword()
    {
        return this.password;
    }
    public boolean validate()
    {
        if((this.userName).equals("john") && (this.password).equals("123abc"))
        {
            return true;
        }
    return false;
    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the username:");
        String name=sc.nextLine();
        System.out.println("Enter the password");
        String pass=sc.nextLine();
        Login obj=new Login(name, pass);
        if(obj.validate())
        {
            System.out.println("Valid user");
        }
        else
        {
            System.out.println("Invalid user");
        }
    }
}
public class Student
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    public void setId(int id)
    {
        this.id=id;
    }
    public int getId()
    {
        return this.id;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return this.name;
    }
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    public int[] getMarks()
    {
        return this.marks;
    }
    public void calculateAvg()
    {   float sum=0;
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
    }
    public void setAverage(float average)
    {
        this.average=average;
    }
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    public float getAverage()
    {
        return this.average;
    }
    public char getGrade()
    {
        return this.grade;
    }
}
  1. Interest Calculator – Interface Concept
  2. Bank – Abstract concept
  3. Inheritance – Account
  4. Contact Details of Hosteller
Sharing Is Caring

Leave a Comment