Question

For this problem, we are going to revisit the Online Company exercise from lesson 3. In...

For this problem, we are going to revisit the Online Company exercise from lesson 3. In lesson 3, we created 3 classes, a superclass Company, a subclass OnlineCompany and a runner class CompanyTester. You can take your solutions from lesson 3 for the Company and OnlineCompany, but we are going to redesign the CompanyTester in this exercise.

Your task is to create a loop that will allow users to enter companies that will then get stored in an ArrayList. You should first prompt users for the company name. If the user enters exit, then the program should exit and print the object using the toString.

After prompting for the name, you prompt the user if it is an online company. If so, ask for a website address, otherwise ask for the street address, city, and state. You will then create the Company or OnlineCompany object and insert it into the ArrayList.

Sample output:

Please enter a company name, enter 'exit' to quit: CodeHS
Is this an online company, 'yes' or 'no': yes
Please enter the website address: www.codehs.com
Please enter a company name, enter 'exit' to quit: Uber
Is this an online company, 'yes' or 'no': no
Please enter the street address: 555 Market Street
Please enter the city address: San Fransisco
Please enter the state address: CA
Please enter a company name, enter 'exit' to quit: exit

CodeHS
Website: www.codehs.com

Uber
555 Market Street
San Fransisco, CA

public class Company {
  
private String name;
private String streetAddress;
private String city;
private String state;

// Set missing values to null
public Company(String name){
this.name = name;
this.streetAddress = null;
this.city = null;
this.state = null;
  
}
public Company(String name, String streetAddress, String city, String state){
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;

}

public String getName(){
return name;
}

/**
* Example output:
* 123 Main St
* Springfield, OH
*/
public String address(){
return streetAddress + '\n' + city + ", " + state;
}

/**
* Example output:
* Widget Company
* 123 Main St
* Springfield, OH
*/
public String toString(){
return name + '\n' + streetAddress + '\n' + city + ", " + state;
}
}

public class OnlineCompany extends Company{

private String webAddress;

public OnlineCompany(String name, String webAddress){
super(name, "it", "does not have", "an address");
this.webAddress = webAddress;
}

//Return the website address
@Override
public String address(){
return webAddress;
}

/**
* Remember To get name from superclass, use super.getName()
*
* Example Output:
* CodeHS
* www.codehs.com
*/
@Override
public String toString(){
return super.getName() + '\n' + webAddress;
  
}
}

## the code below is the one I need to complete

import java.util.*;

public class CompanyTester
{
public static void main(String[] args)
{
// Start here!
}
}

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

Please upvote ,comment for any query ,Thanks.

Note : check attached image for output and program .code compiled and tested in netbeans java.

Program : *******************CompanyTester.Java **********************************

import java.util.*;

public class CompanyTester
{
public static void main(String[] args)
{
Scanner scan =new Scanner(System.in); //scanner object
  
List<Object> ListObject=new ArrayList<>(); //create array list
  
while(true)
{
  
String companyName,companyType,webAddress,city,state,street; //attribute for company name
System.out.print("enter a company name or \"exit\": "); //prompt user
companyName=scan.nextLine();
if(companyName.equalsIgnoreCase("exit")) //if user type exit print all company name and attribute
{
for(Object o : ListObject) //first to last object
{
System.out.println(o.toString()); //print attribute and name
}
break; //exit from loop
}
  
System.out.print(companyName+" is an online company, \"yes\" or \"no\" ");
companyType=scan.nextLine(); //get type
if(companyType.equals("yes")) //if company is online add webaddress not location
{
System.out.print("Enter web address of company "+companyName+" :");
webAddress=scan.nextLine();
OnlineCompany object1= new OnlineCompany(companyName, webAddress); //create object
  
  
ListObject.add(object1); //add object to object list
  
  
}
else if(companyType.equalsIgnoreCase("no")) //if company is offline
{
System.out.print("Enter street address of "+companyName+" :"); //get street
street=scan.nextLine();
System.out.print("Enter city name of "+companyName+" :");
city=scan.nextLine();
System.out.print("Enter state name of "+companyName+" :");
state=scan.nextLine();
  
  
Company companyObject = new Company(companyName, street,city,state); //create object of company
  
ListObject.add(companyObject); //add to list
}
  
  
  
}

}
}

Program : ***************************************Company.java**********************

public class Company {
  
private String name;
private String streetAddress;
private String city;
private String state;

// Set missing values to null
public Company(String name){
this.name = name;
this.streetAddress = null;
this.city = null;
this.state = null;

}
public Company(String name, String streetAddress, String city, String state){
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;

}

public String getName(){
return name;
}

/**
* Example output:
* 123 Main St
* Springfield, OH
* @return
*/
public String address(){
return streetAddress + '\n' + city + ", " + state;
}

/**
* Example output:
* Widget Company
* 123 Main St
* Springfield, OH
*/
@Override
public String toString(){
return name + '\n' + streetAddress + '\n' + city + ", " + state;
}
}

Program : *************OnlineCompany.java*********************************

public class OnlineCompany extends Company{

private String webAddress;

public OnlineCompany(String name, String webAddress){
super(name, "it", "does not have", "an address");
this.webAddress = webAddress;
}

//Return the website address
@Override
public String address(){
return webAddress;
}

/**
* Remember To get name from superclass, use super.getName()
*
* Example Output:
* CodeHS
* www.codehs.com
*/
@Override
public String toString(){
return super.getName() + "\nwebsite:" + webAddress;

}
}

Output :

Building Online Company 1.0-SNAPSHOT - --- exec-maven-plugin:1.5.0:exec (default-cli) @ Online Company --- enter a company na

Please upvote

Add a comment
Know the answer?
Add Answer to:
For this problem, we are going to revisit the Online Company exercise from lesson 3. In...
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
  • write a parent class that describes a parcel (like a package, as shown below) and a...

    write a parent class that describes a parcel (like a package, as shown below) and a child class that describes an overnight parcel. Use the provided Address class. A parcel is described by: id (which might contain numbers and letters) weight (described as the number of pounds; a parcel could be less than 1 pound) destination address (uses the provided class) An overnight parcel is described by id, weight, destination address and also: whether or not a signature is required...

  • In Java Programming Complete the Code that remains to be implemented follow the guide lines here...

    In Java Programming Complete the Code that remains to be implemented follow the guide lines here and finish what code remains name of program class Student.java package course; public class Student { private String firstName; private String lastName; private Address homeAddress; private Address schoolAddress; private double[] testScores; // Four parameter constructor public Student(String firstName, String lastName, Address homeAddress, Address schoolAddress) { // Call the seven parameter constructor. Pass zeros for the three grades } // Five parameter constructor public Student(String...

  • Debugging exercise Programmer Began programming the following program, but made some mistakes. Please debug: class Student{...

    Debugging exercise Programmer Began programming the following program, but made some mistakes. Please debug: class Student{     private int id;     private String name;     private String city;       Student(String name, String city){         this.name = name;         this.city = city;     }        Student(int id, String place){         this.id = id;         this.place =name;     }        void Student(int id, String name, String city){         this.id = id;         this.name = name;         this.city =...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • The ", delimiter" should now be changed to use the "^ delimiter". There needs to be...

    The ", delimiter" should now be changed to use the "^ delimiter". There needs to be an 2nd address field added to the original code, as well as, a "plus 4" on the zip code (example: 47408-6606). Then the additional prompts must be added. Thank you! Last Real-World problem (in module 04 - 05), you added functionality to allow the user to enter multiple patients until the user indicated done. You are to enhance this functionality. Add the following functionality...

  • How can the java code be edited that when someone exits the lot, they receive an...

    How can the java code be edited that when someone exits the lot, they receive an exiting timestamp. And also i would need that exit timestamp to be subtracted from the timestamp the driver gets when entering the lot to find out the total amount of time they were in the parking lot. please explain. should only need a couple of lines added. //////////////////////////////ParkingCarInfo.java/////////////////////////////////////////// package test; import java.sql.Timestamp; //Class: ParkingCarInfo public class ParkingCarInfo { private String name; //name private Timestamp...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • The popular social network Facebook TM was founded by Mark Zuckerberg and his classmates at Harvard...

    The popular social network Facebook TM was founded by Mark Zuckerberg and his classmates at Harvard University in 2004. At the time, he was a sophomore studying computer science. Design and implement an application that maintains the data for a simple social network. Each person in the network should have a profile that contains the person's name, an image(extra credit), current status(Online, offline, busy,...), and a list of friends. Your application should allow a user to join the network, leave...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

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