Question

In: Computer Science

In Java, Here is a basic Name class. class Name { private String first; private String...

In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of the method is to see whether the given name exists in names. If yes, return true; if not, return false. (Use the equals method.) Note that names might not be full, so numNames tells us how many Names are stored in the array and therefore how far in the array we should search. (In other words, it could be that names.length > numNames.) Write this method.

Solutions

Expert Solution

class Name {
        private String first;
        private String last;

        public Name(String first, String last) {
                this.first = first;
                this.last = last;
        }

        public boolean equals(Name other) {
                return this.first.equals(other.first) && this.last.equals(other.last);
        }
}

public class TestNames {
        public static boolean exists(Name[] names, int numNames, Name name) {

                // iterating through given array and checking if has the given name 
                //using equals method
                for (int i = 0; i < numNames; i++) {
                        if (names[i].equals(name))
                                return true;
                }
                return false;
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/ private String lastName; 4/ private double balance; 5/ public Account(long accountNumber, String firstName, String lastName, double balance); 6/ get/set methods for all attributes. 7/ public boolean deposit(double amount);   • deposit only if amount is greater than 10 but   less than 200, add the deposit   amount   to the   currentbalance. • if deposit is   successful return true,   otherwise return false; 8/ public boolean withdrawal(double amount); •...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT