0

I have a Java Class named Real

public class Real {
  private long wholeNumPart;
  private long decimalPart;
  public Real(){
        wholeNumPart =0;
        decimalPart=0;
    }
    public Real(long wholeNumPart, long decimalPart) {
        this.wholeNumPart =wholeNumPart;
        this.decimalPart = decimalPart;
    }
    
    public long getWholeNumPart() {
        return wholeNumPart;
    }
    public long getDecimalPart() {
        return decimalPart;
    }}

I have another class name RealApplication where I need to create two methods

  1. createRealObject() that allows a user to input a real number and creates an object representing that number. 2.createRealNumber() which accepts an object of type Real and returns a real number represented by that object.

I am having difficulty creating these two methods Here is what I've done so far

import java.util.Scanner;
public class RealApplication {
    
    public void createRealNumber() {
        Scanner sc=new Scanner(System.in);
        
        //Allows user input
        System.out.print("Please, enter a real number: ");
        long n = sc.nextLong();
        
        
        //Creates Real object ( is this correct????)
        Real in = new Real();
        
    }
    
    public long createRealNumber(Real num) {
        long realNum=0;

        //I do not know what to write here :(

        return realNum;
    }
}
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Jake
  • 21
  • 5
  • You are asking the user for an input but you don't use the input. Why that? – akuzminykh Feb 04 '21 at 23:18
  • Why do you have this in the constructor? `public Real(long wholeNumPart, long decimalPart)` Are you trying to storage in a **Real** object something like this?: **Real number: 10.23** `wholeNumPart=10` , `decimalPart=23` – Edgar Magallon Feb 04 '21 at 23:19
  • @akuzminykh I don't know how to use this input. I need to create an object representing that number but i dont know how to do it. – Jake Feb 04 '21 at 23:24
  • @EdgarMagallon that's right.. it has to separate the whole and decimal part – Jake Feb 04 '21 at 23:24
  • 1
    @Jake Check out [this](https://www.w3schools.com/java/java_constructors.asp). – akuzminykh Feb 04 '21 at 23:27
  • @Jake you have to read about Constructors, parameters. Please read that link that Jake recommends, it will help you. And to separate the integer and decimal part of the input, you should try read the input as a String, for example: `String input = sc.next()` Or you can try a cast, check out [this link](https://stackoverflow.com/questions/13044079/separating-double-into-integer-and-decimal-parts), it may help you. – Edgar Magallon Feb 04 '21 at 23:35
  • @akuzminykh ahh okay i've understood the first method..and the second one..how do i proceed? :/ – Jake Feb 05 '21 at 07:43

1 Answers1

0

Your Real class looks good with some changes in the RealApplication class we can achieve what you want:

import java.util.Scanner;
public class RealApplication {

public static Real createRealObject() {
    Scanner sc=new Scanner(System.in);

    //Allows user input
    System.out.print("Please, enter a real number: ");
    String n = sc.nextLine();
    String[] pieces = n.split("[.,]+");     //special pattern to recognize comma and dot
    long wholeNumPart;
    long decimalPart;
    try {
        wholeNumPart = Long.valueOf(pieces[0]);
        decimalPart = Long.valueOf(pieces[1]);
    }
    catch (NumberFormatException e) {
        System.out.println("You should enter a number!");
        return null;
    }
    Real in = new Real(wholeNumPart, decimalPart);
    sc.close();
    return in;
}

The important point is that I declared the both methods as static in this way you can use the methods without creating an instance of RealApplication class.

You should use "double" primitive type to store fractional numbers not "long". Now the method that returns the number equivalent of that object:

public static double createRealNumber(Real num) {
    double realNum;
    long wholeNumPart = num.getWholeNumPart();
    long decimalPart = num.getDecimalPart();
    int numOfDigits = (int)(Math.log10(decimalPart)+1);

    realNum = wholeNumPart + (decimalPart / Math.pow(10,numOfDigits));

    return realNum;
}

And if we write a main method:

public class Main {

public static void main(String[] args) {



    Real realObj = new Real(10,2323232);

    double number = RealApplication.createRealNumber(realObj);

    System.out.println("Equivalent number for the object: " + number);


    Real newObj = RealApplication.createRealObject();

    if (newObj != null) {
        System.out.println("New object's number part: " + newObj.getWholeNumPart());
        System.out.println("New object's decimal part: " + newObj.getDecimalPart());
    }
    else{
        return;
    }

  }
}

Because of the regex pattern we used, the inputs separated with "." and "," are allowed like 10.23 10,23 .