简体   繁体   中英

Java - Problems with overload constructor containing only a string

I am having trouble figuring out how to correctly ask the user for 3 inputs representing attributes that will be used to create an object which will be printed on the screen and making sure the inputs aren't bad values. For example, if the user typed in .5 as a diameter it would change to 1. The overload constructor is supposed to represent the name of a file which will contain the name, diameter, and temperature of the object being created.

When I try to run it there would errors for missing symbols. How can I get it so that the overload constructor with string as its only parameter to be called into the Object2 class in order to create an object?

public class Object1
{
   //attributes
   private double yyy;
   private int zzz;
   private String xxx;
   private String fileName;

   //symbolic constants
   private final double MINDIAMETER = 1;
   private final int MINTEMP = 300;
   private final String NA = "N/A";

   // getter/setter methods for name
   public void setName(String n)
   {
   if (n != "")
   xxx = n;

   else 
   xxx = NA;
   }

   public String getName()
   {
   return xxx;
   }

   // getter/setter methods for diameter
   public void setDiameter(double d)
   {
   if (d >= MINDIAMETER)
   yyy = d;

   else 
   yyy = MINDIAMETER;
   }

   public double getDiameter()
   {
   return yyy;
   }

   // getter/setter methods for temperature
   public void setTemp(int t)
   {
   if (t >= MINTEMP)
   zzz = t;

   else 
   zzz = MINTEMP;
   }

   public int getTemp()
   {
   return zzz;
   }

   public Object1()
   {
      xxx = NA;
      yyy = MINDIAMETER;
      zzz = MINTEMP;
   }

   public Object1(String n, double d, int t)
   {
      setName(n);
      setDiameter(d);
      setTemp(t);
   }

   public Object1(String f)
   {
        fileName = f; //Initialize the object's fileName with f
        System.out.print("Enter the filename: ");


   }

   public String toString()
   {
      return "A object, named " + xxx + ", with a diameter of " + yyy + " miles, and a temperature of " + zzz + " Kelvin.";
   }
}

Seperate file code

import java.util.Scanner;
import java.io.FileWriter;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.IOException;

public class Object2
{
   public static void main(String[] args) throws IOException
   {

         // object with overload
        Object1 z;
        z = new Object1("");

        Scanner kb = new Scanner(System.in);


        fileName = kb.nextLine();
        File file = new File(fileName);
        PrintWriter outputFile = new PrintWriter(file);

        System.out.println("please enter name: ");
        while (kb.hasNext())
        {

        name = kb.nextLine();
        System.out.println("please enter the diameter: ");
        diameter = kb.nextDouble();
        System.out.println("please enter the temperature: ");
        temp = kb.nextInt();

        System.out.println(name + " " + diameter + " " + temp);

        outputFile.println(name);
        outputFile.println(diameter);
        outputFile.println(temp);
        }


        outputFile.close();
        System.out.println(z);
    }
}

First there is something have to change in your Object1 class.

public class Object1
{
   ...

   public void setName(String n)
   {
   if (!n.equals(""))
   xxx = n;

   else 
   xxx = NA;
   }

   ...
}

Secondly you need to implement your scanner values to your object.

z.setName(kb.nextLine());
System.out.println("please enter the diameter: ");
z.setDiameter(kb.nextDouble());
System.out.println("please enter the temperature: ");
z.setTemp(kb.nextInt());

System.out.println(z.getName + " " + z.getDiameter + " " + z.getTemp);

outputFile.println(z.getName);
outputFile.println(z.getDiameter);
outputFile.println(z.getTemp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM