Question

In Java,enhace the program base on the first one. 1. You are going to design (and...

In Java,enhace the program base on the first one.

1. You are going to design (and code) a class called Name. The class Name will contain three properties:

  • first, the first name which is a String
  • initial, the middle initial, which is a single character.
  • last, the last name, which is a String

The class has three constructors:

  • A default constructor, which accepts no parameters, calls constructors for the first and last name and sets the initial equal to a blank space.
  • Two conversion constructors, one of which accepts two parameters (first and last name), the other accepting three parameters (first name, middle initial and last name). The constructor accepting two paraeters sets the middle initial equal to a blank space.

In addition to the constructors, the Name class has the following methods:

  • readName() - which prompts the user for first name, middle initial and last name and reads them in.
  • writeName() - which prints the first name, middle initial and last name with a blank separating them, but without a newline (Use System.out.print().)
  • equals(), which returns true if the names are the same, false otherwise (case should match as well).

Write another class complete with a main()that uses the new class that you wrote and demonstrates that these methods all work correctly.

2. In the last assignment, we created a class called Name. This time, we are going to add the following methods:


toString - which returns a String consisting of the name.

A copy constructor, which takes as its one parameter another object of class Name

precedes, which returns true if the object comes before the parameter

Create a main program that reads in three names and prints the name that is first in standard alphabetical order (by last name; if the last name is the same by first name; if both match by initial).

And create a program that reads in three names and prints them in the order in which they should appear. Hint: Write a method that takes all three names and returns the name that appears first, a method that takes all three names and returns the name that appears in last.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:


import java.util.*;
class Name{
   String first;
   char initial;
   String last;
   Name(){
       initial=' ';
   }
   Name(String first,String last){
       this.first=first;
       this.last=last;
       initial=' ';
   }
   Name(String first,char initial,String last){
       this.first=first;
       this.last=last;
       this.initial=initial;
   }
   void readName() {
       Scanner s=new Scanner(System.in);
       System.out.print("Enter first name: ");
       first=s.next();
       System.out.print("Enter middle initial: ");
       initial=s.next().charAt(0);
       System.out.print("Enter last name: ");
       last=s.next();
       s.close();
   }
   void writeName() {
       System.out.print(first+" ");
       System.out.print(initial+" ");
       System.out.print(last+"\n");
   }
   boolean equals() {
       if(first.equals(last)) {
           return true;
       }
       else {
           return false;
       }
   }
   public String toString() {
       return first+" "+initial+" "+last;  
   }
}
public class Complete {

   public static void main(String[] args) {
       Name a=new Name();
       a.readName();
       a.writeName();

   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
In Java,enhace the program base on the first one. 1. You are going to design (and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • use Java and it must work for any name String Manipulator Write a program to manipulate...

    use Java and it must work for any name String Manipulator Write a program to manipulate Strings. Store your full name into one String variable. It must include first name, middle name, last name, cach separated by spaces. Spaces could vary. If you do not have a middle name make up one For example the string to be processed could be any of the following John Plain Doe John Plain Doc John Plain Doe Your program must be able to...

  • Java: Create a class called Vehicle with the following features: a. It has three private data...

    Java: Create a class called Vehicle with the following features: a. It has three private data members (instance variables): one is the manufacturer’s name (String manufacturer), the second is the number of cylinders in the engine (int cylinder), and the third is the owner (Person owner). The class Person is described above. b. It has three constructors, a no-argument constructor, a constructor with three parameters, and a copy constructor. The no-argument constructor will set the manufacturer to “None”, cylinder to...

  • This is a java file and I am very confused on how to do this project!...

    This is a java file and I am very confused on how to do this project! please help!! Specifications Overview: You will write a program this week that is composed of three classes: the first class defines Ellipsoid objects, the second class defines EllipsoidList objects, and the third, Ellipsoid ListApp, reads in a file name entered by the user then reads the list name and Ellipsoid data from the file, creates Ellipsoid objects and stores them in an ArrayList of...

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Copy the entire program 9A and paste it into this one. Be sure to change the...

    Copy the entire program 9A and paste it into this one. Be sure to change the class name to 9B. Use descriptive variable names in camel-case with the first letter in lowercase. In the Student class, change the public attributes firstName and lastName to private ones. In the Student class, add getters and setters called getFirstName, getLastName, setFirstName, setLastName for the two attributes. In the Student class, add a constructor for the Student class that accepts two Strings, one parameter...

  • 2- Write a program that has a Person class, a Student class, that is derived from...

    2- Write a program that has a Person class, a Student class, that is derived from Person, and a Faculty class, derived from Person as well, with the following specifications: The Person class has the following members: a. An integer private member variable named SSN. b. Two string protected member variables named firstName and lastName. C. An integer protected member variable named age. d. A constructor that takes SSN, first name, last name, and age as parameters. e. A constructor...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT