PLEASE WRITE IN JAVA FOR Q's A & B: Three investors are starting a new startup. Each investor has a first name, last name and phone number all of type String and the amount of investment dollars of type double.
A.) Write an Investor class that includes the above information. 1.) Make the instance variable of the investor class private. 2.) Write getter and setter methods for each instance variable. 3.) Add a constructor method to the investor class that receives values for all instance variable as parameters.
B.) Write an InvestorTest class that creates an array of Investor objects to save the information of the three investors. 1.) Use a loop to read the values for each investor objects from the keyboard and create the objects. 2.) Use an enhanced for loop to print the values of the instance variables of each investor. 3.) Write a method called percentages which receives the array of investors as a parameter and returns an ArrayList of double numbers that contains the percentage of shares of each investor. 4.) Call the percentages method and print its returned values.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Investor.java
public class Investor {
// fields
private String firstName;
private String lastName;
private String phoneNumber;
private double investment;
// constructor to initialize all fields
public Investor(String firstName, String lastName, String phoneNumber,
double investment) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.investment = investment;
}
// getters and setters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public double getInvestment() {
return investment;
}
public void setInvestment(double investment) {
this.investment = investment;
}
}
// InvestorTest.java
import java.util.ArrayList;
import java.util.Scanner;
public class InvestorTest {
// method to calculate the percentages of shares of each investor
public static ArrayList<Double> percentages(Investor investors[]) {
// array list to store the results
ArrayList<Double> percents = new ArrayList<Double>();
double sum = 0;
// looping through array once, and summing all the investment values to
// get total investment
for (Investor inv : investors) {
sum += inv.getInvestment();
}
// looping through the array again
for (Investor inv : investors) {
// finding investment percentage, adding to array list
double sharePercent = (inv.getInvestment() / sum) * 100.0;
percents.add(sharePercent);
}
return percents;
}
public static void main(String[] args) {
// creating an array of 3 investors
Investor array[] = new Investor[3];
Scanner scanner = new Scanner(System.in);
// looping and reading values and filling array
for (int i = 0; i < array.length; i++) {
System.out.print("Enter first name of investor #" + (i + 1) + ": ");
String first = scanner.nextLine();
System.out.print("Enter last name of investor #" + (i + 1) + ": ");
String last = scanner.nextLine();
System.out.print("Enter phone number of investor #" + (i + 1)
+ ": ");
String phone = scanner.nextLine();
System.out.print("Enter investment amount of investor #" + (i + 1)
+ ": ");
double amt = Double.parseDouble(scanner.nextLine());
// creating an Investor with entered values
Investor investor = new Investor(first, last, phone, amt);
array[i] = investor;
}
// using an enhanced for loop, printing data of each investor
for (Investor inv : array) {
System.out.println("investor first name: " + inv.getFirstName());
System.out.println("investor last name: " + inv.getLastName());
System.out
.println("investor phone number: " + inv.getPhoneNumber());
System.out.println("Investment amount: " + inv.getInvestment());
System.out.println();
}
System.out.println("Investment percentages: ");
int i = 1;
// getting percentages, looping through each percentage
for (double p : percentages(array)) {
// displaying investor number and sharee percentage formatted to 2
// decimal places
System.out.printf("investor #%d: %.2f%%\n", i, p);
i++;
}
}
}
/*OUTPUT*/
Enter first name of investor #1: Oliver
Enter last name of investor #1: Queen
Enter phone number of investor #1: 123-999
Enter investment amount of investor #1: 50000
Enter first name of investor #2: John
Enter last name of investor #2: Diggle
Enter phone number of investor #2: 444-1627
Enter investment amount of investor #2: 25000
Enter first name of investor #3: Barry
Enter last name of investor #3: Allen
Enter phone number of investor #3: 444-4444
Enter investment amount of investor #3: 25000
investor first name: Oliver
investor last name: Queen
investor phone number: 123-999
Investment amount: 50000.0
investor first name: John
investor last name: Diggle
investor phone number: 444-1627
Investment amount: 25000.0
investor first name: Barry
investor last name: Allen
investor phone number: 444-4444
Investment amount: 25000.0
Investment percentages:
investor #1: 50.00%
investor #2: 25.00%
investor #3: 25.00%
PLEASE WRITE IN JAVA FOR Q's A & B: Three investors are starting a new startup....
Write method createTeams() that Has no parameters Has return type void Instantiate the member variable of type ArrayList<Team> Instantiates two instances of class Team one for TeamOne one for TeamTwo set names for each team as appropriate add each instance to the ArrayList<Team> member variable Instantiate the member variable of class Scanner passing “System.in” as the argument to the constructor Using System.out.println() static method, prompt the user for the human player’s name Instantiate an instance of class String set equal...
cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...
Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store { public final double SALES_TAX_RATE = 0.06; private String name; /** * Constructor to create a new store * @param newName */ public Store(String newName) { name = newName; } ...
Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
I want to write a superclass with one private attribute and one method in JAVA The super class will later derive into 2 subclasses each with its own private attributes and each with a method that OVERRIDES the superclass method with each subclasses with one method ( that will do something) unique to that subclass. ALL classes must have constructors initializing its attributes and getter/setter methods. Then, in the main method, keep initializing objects of type superclasses based on user's...
Write a program in C++ with comments! Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered. Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter...
JAVA Problem: Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...
WRITE THE PROGRAM IN JAVA. gram 3: Olympians I C 1 Goals To write your own class in a program To write a constructor with parameters, an method and a toString) method. To use ArrayList To use a for-each loo . p *l . To use text file for output 2 The Context This is a single-class project, the first for which I have not given you code to start from. It is not realistic, or useful, but it gives...
In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...