Question

In: Computer Science

1. Create a1. Create a new java file named Name.java that should have Name class based...

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 !

Solutions

Expert Solution

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!


Related Solutions

THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT