Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:46:20+00:00 2026-06-18T03:46:20+00:00

I am trying to code a program that can take user input data about

  • 0

I am trying to code a program that can take user input data about a carpet, parse the string into the necessary pieces of information and create carpet objects based off the shape. My code is

    public class CarpetParser{

    public static Carpet parseStringToCarpet(String lineToParse)
    {
        String delims = "[/]";
        String[] info = lineToParse.split(delims);
        if(info[0].equalsIgnoreCase("rectangle")){
            double priceFor = Double.parseDouble(info[2]);
            int height = Integer.parseInt(info[3]);
            int width = Integer.parseInt(info[4]);
            RectangleCarpet theCarpet = new RectangleCarpet(info[1], priceFor, height, width);
            return theCarpet;

        }else if(info[0].equalsIgnoreCase("circle")){
            double priceFor = Double.parseDouble(info[2]);
            int radius = Integer.parseInt(info[3]);
            CircleCarpet theCarpet = new CircleCarpet(info[1], priceFor, radius);
            return theCarpet;

        }

    }
}

for the parser,

    public abstract class Carpet{

    protected int area = 0;
    protected double unitPrice = 0;
    protected double totalPrice = 0.0;
    protected String carpetID;

    public Carpet(String ID, double thisPrice){
        carpetID = ID;
        unitPrice = thisPrice;
    }

    public String getCarpetId(){
        return carpetID;
    }

    public String toString(){
        String carpet = new String("\n" + "The CarpetId:\t\t" + getCarpetId() + "\nThe Area:\t\t" + area + "\nThe Unit Price\t\t" + unitPrice + "\nThe Total Price\t" + totalPrice + "\n\n");
        return carpet;
    }

    public abstract void computeTotalPrice();

}

for the carpet,

    public class RectangleCarpet extends Carpet{

    private int height;
    private int width;

    public RectangleCarpet(String ID, double priceOf, int h, int w){
        super(ID, priceOf);
        height = h;
        width = w;
        computeTotalPrice();
    }

    public void computeTotalPrice(){
        super.area = height * width;
        super.totalPrice = unitPrice * area;
    }

    public String toString(){
        String forThis = new String("\nThe Carpet Shape:\tRectangle\nThe Height:\t\t" + height + "\nThe Width:\t\t" + width +"\n");
        return forThis + super.toString();

    }

}

for one of the carpet shapes and

    public class CircleCarpet extends Carpet{

    private int radius;

    public CircleCarpet(String ID, double priceOf, int rad){
        super(ID, priceOf);
        radius = rad;
        computeTotalPrice();

    }

    public void computeTotalPrice(){
        super.area = radius * radius * 3;
        super.totalPrice = area * unitPrice;
    }


    public String toString(){
        String forThis = new String("\nThe Carpet Shape:\tCircle\nThe radius:\t\t" + radius + "\n");
        return forThis + super.toString();
    }

}

for the other shape. The problem is the parseStringToCarpet is missing a return value, and I can’t figure out what it needs to return, because if I try to return theCarpet it says it is the wrong type.

The calling class is

`import java.io.*;         //to use InputStreamReader and BufferedReader
import java.util.*;       //to use ArrayList

public class Menu
 {
  public static void main (String[] args)
   {
     char input1;
     String inputInfo = new String();
     String line = new String();
     boolean found;

     // ArrayList object is used to store carpet objects
     ArrayList carpetList = new ArrayList();

     try
      {
       printMenu();     // print out menu

       // create a BufferedReader object to read input from a keyboard
       InputStreamReader isr = new InputStreamReader (System.in);
       BufferedReader stdin = new BufferedReader (isr);

       do
        {
         System.out.println("What action would you like to perform?");
         line = stdin.readLine().trim();
         input1 = line.charAt(0);
         input1 = Character.toUpperCase(input1);

         if (line.length() == 1)
          {
           switch (input1)
            {
             case 'A':   //Add Carpet
               System.out.print("Please enter a carpet information to add:\n");
               inputInfo = stdin.readLine().trim();
               carpetList.add(CarpetParser.parseStringToCarpet(inputInfo));
               break;
             case 'C':   //Compute Total Price For Each Carpet
               for (int i=0; i<carpetList.size();i++)
                     ((Carpet) carpetList.get(i)).computeTotalPrice();
               System.out.print("total prices computed\n");
               break;
             case 'D':   //Search for Carpet
               System.out.print("Please enter a carpetID to search:\n");
               inputInfo = stdin.readLine().trim();
               found = false;
               for (int i=0; i<carpetList.size();i++)
                {
                 if (inputInfo.equals(((Carpet)carpetList.get(i)).getCarpetId()))
                  {
                   found = true;
                  }
                }
                if (found == true)
                 System.out.print("carpet found\n");
                else
                 System.out.print("carpet not found\n");
               break;
             case 'L':   //List Carpets
               if (carpetList.isEmpty())
                System.out.print("no carpet\n");
               else
                for (int i=0; i < carpetList.size(); i++)
                  System.out.print(carpetList.get(i).toString());
               break;
             case 'Q':   //Quit
               break;
             case '?':   //Display Menu
               printMenu();
               break;
             default:
               System.out.print("Unknown action\n");
               break;
            }
         }
        else
         {
           System.out.print("Unknown action\n");
          }
        } while (input1 != 'Q'); // stop the loop when Q is read
      }
     catch (IOException exception)
      {
        System.out.println("IO Exception");
      }
  }

  /** The method printMenu displays the menu to a use **/
  public static void printMenu()
   {
     System.out.print("Choice\t\tAction\n" +
                      "------\t\t------\n" +
                      "A\t\tAdd Carpet\n" +
                      "C\t\tCompute Total Price For Each Carpet\n" +
                      "D\t\tSearch for Carpet\n" +
                      "L\t\tList Carpets\n" +
                      "Q\t\tQuit\n" +
                      "?\t\tDisplay Help\n\n");
  }
}

` I’m not allowed to edit the code of the calling class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:46:21+00:00Added an answer on June 18, 2026 at 3:46 am

    You always have to make sure all the paths in a method with returning value have a return in it or throw exception. In this case, you could add:

     else {
                return null;
            }
    

    to the last part of the method parseStringToCarpet, or just write return null at the end of the method.

    The problem returning null is that a method that calls this function should know that it might return null, so you should document it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a program that takes a text file of c++ code
I am trying to improve my C++ by creating a program that will take
I am trying to write a method that can take a starting lowercase letter
In a program that I'm trying to write now I take two columns of
I have a code that creates file(s) in user-specified directory. User can point to
I'm trying to write a program that will take two numbers and depending on
I am trying to write a fragment program that will take a texture and
I've recently run into a mild problem when trying to program a class that
I'm trying to write a program to test student code against a good implementation.
I'm trying to write more efficient code in a C program, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.