In: Computer Science
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML
Name
- first: String
- last: String
+ Name ()
+ Name (String firstName, String lastName)
+ getName () : String
+ setName (String firstName, String lastName) : void
+ getFirst () : String
+ setFirst (String firstName) : void
+ getLast () : String
+ setLast (String lastName) : void
+ printName () : void
Notes: Name() constructor should call the other constructor to set blank values for first and last variables; getName should call getFirst and getLast; setName should call setFirst and setLast; printName should call getName and then print the name in the following format (assuming that values of Rahul & Dewan were used to create the Name object) – Hello Rahul Dewan, Welcome to CIS1500 Course ! If the name is blank, it should display message like: Name is blank !
Hi
Please find the code below
I have also attached the snapshot of the output
//The third compartment represents methods on the class. Attributes and operations can be preceded with a visibility adornment. A plus sign (+) indicates public visibility. A minus sign (-) denotes private visibility
import java.util.*;
class Name{
// - sign denotes private fields
private String first;
private String last;
// + sign denotes public avalibility for fields, methods
public Name(){
// way to call another constructor from a constructor
//set blank values for first and last name
// as per the given condition
this("","");
}
public Name(String firstName, String lastName){
this.first= firstName;
this.last = lastName;
}
public String getName(){
// call getFirstName and getLastName
String firstName = getFirst();
String lastName = getLast();
//return name
return firstName + " " + lastName;
}
public String getFirst(){
return this.first;
}
public String getLast(){
return this.last;
}
public void setFirst (String firstName){
// set firstName
this.first = firstName;
}
public void setLast (String lastName){
// set lastName
this.last = lastName;
}
public void printName (){
//call getName and print the name
String PersonName = getName();
if(PersonName == "")
System.out.println("Name is blank
!");
else
System.out.println("Hello " +
PersonName + ", Welcome to CIS1500 Course !");
}
}
This is Other Test.java to demonstrate the working of NAME.java
import java.util.*;
public class Test{
public static void main(String[] args) {
Name test = new Name("Rahul","Dewan");
test.printName();
}
}
OUTPUT:
Thanks Hope it helps!