Question

I Need Help with this using Java Programming :

Class name fname Iname Class variable total Number Constructor (no arguments) Constructor (takes two arguments, fname and Ina

Class name

fname

lname

Class variable

total Number

Constructor (no arguments)

Constructor (takes two arguments, fname and lname)

One getter method to return the fname and lname

One setter method for fname and lname

Inside Main:

Create three objects with the following names

Jill Doe

John James

Jack Smith

When creating the first object (Should not be an anonymous object)

use the argument-less constructor

Then use the setter method to assign the fname and lname

Then use the getter method to display the information for the first object

When creating the second object (Should not be an anonymous object)

use the two argument constructor to set the fname and lname

then use the getter method to display the information for the second object

When dealing with he third object (All of this should happen using one single statement)

Create an anonymous, third object using the two argument constructor. Use the getter method to retrieve fname and lname as a single string and then display the string. Remember that all of this code should be in a single statement.

Every time you add an abject increment total number by 1. This increment should only happen in the argument-less constructor’s tour. This means that if you call the two argument constructor, you have to revere to the argument-less constructor as well.

Before the end of the program, display the number of objects that were created by displaying the contents of totalNumber

Thank You :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Name {
        private String fname, lname;
        public static int totalNumber = 0;

        public Name() {
                fname = "DEFAULT";
                lname = "DEFAULT";
                totalNumber++;
        }

        public Name(String fname, String lname) {
                this();
                this.fname = fname;
                this.lname = lname;
        }

        public void setName(String fname, String lname) {
                this.fname = fname;
                this.lname = lname;
        }

        public String getName() {
                return this.fname + " " + this.lname;
        }
}

public class NameDemo {

        public static void main(String[] args) {
                Name n1 = new Name();
                n1.setName("Jill", "Doe");
                System.out.println(n1.getName());
                
                Name n2 = new Name("John", "James");
                System.out.println(n2.getName());
                
                System.out.println(new Name("Jack", "Smith").getName());
                
                System.out.println("total objects created: " + Name.totalNumber);
        }

}

2 class Name 1 Jill Doe John James Jack Smith total objects created: 3 private String fname, lname; public static int totalNu
Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
I Need Help with this using Java Programming : Class name fname lname Class variable total...
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
  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • **Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class...

    **Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class must be in separate file. Draw an UML diagram with the inheritance relationship of the classes. 1. The Athlete class a. All class variables of the Athlete class must be private. b.is a class with a single constructor: Athlete(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender). All arguments of the constructor should be stored as class variables. There should only...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class....

    Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class. The first constructor will take zero input arguments and set the stuff attibute to null. The second will take a String as input and set the shul atbute to input string Getter and Setter Write a public getter method called getsauff that returns the string stored by the sthuff attribute If stulf is null, it should return the string "no stuff in here Write...

  • This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...

    This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection. The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file. The Tweet Class The Tweet class consist of nine members that include two static ones (the members decorated with the...

  • Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

    Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin {    /**     * Constructor for objects of class...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

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
ADVERTISEMENT