for exercise I had to create a class with four constructor, two get methods and three set methods. I then had to create a program to test it. My problem is that the test program won't read the input correctly, as I have to give "input 1", then 2/3 spaces and then "input 2" for it to work.
Also, the two get methods don't work.
This is my Test program:
import java.util.*;
public class TemperaturaTest {
static Scanner sc = new Scanner (System.in);
public static void main (String args []){
System.out.println("Inserisci i valori di temperatura e la scala (C o F).");
int gradi = sc.nextInt();
sc.nextLine();
String scala = sc.nextLine();
Temperatura temp1 = new Temperatura(gradi, scala);
System.out.println(temp1.getCelsius());
System.out.println(temp1.getFahrenheit());
}
}
And this is the class:
import java.util.*;
import java.io.*;
public class Temperatura {
private static double temp;
private static String scala;
//Costruttori
public Temperatura(){
temp=0;
scala="C";
}
public Temperatura(double gradi){
setGradi(gradi);
scala="C";
}
public Temperatura(String scalagradi){
temp=0;
setScala(scalagradi);
}
public Temperatura(double gradi, String scalagradi){
setGradi(gradi);
setScala(scalagradi);
}
//Metodi set
private void setGradi(double gradi){
temp=gradi;
}
private void setScala(String scalagradi){
if (!(scalagradi.equalsIgnoreCase("C") || scalagradi.equalsIgnoreCase("F"))){
System.out.println("Puoi usare solo le lettere C (Celsius) ed F (Fahrenheit) per indicare la temperatura. ");
System.exit(0);
}
scala = scalagradi;
}
private void setTemp(double gradi, String scalagradi){
setGradi(gradi);
setScala(scalagradi);
}
//Metodi get
public double getCelsius(){
if (scala.equalsIgnoreCase("C")){
return temp;
}
return (5*(temp-32)/9);
}
public double getFahrenheit(){
if (scala.equalsIgnoreCase("F")){
return temp;
}
return ((9*(temp)/5)+32);
}
}
If I use as input a random number and then, after 2/3 spaces, C or F, the program ends without error (but also without the two get methods working). If I use another letter (not C or F) it gives me the error I set in the setScala() method, so the input works (except for the fact that I have to use spaces)
I have these 4 constructors because after I get this to work I also have to test how Java automatically uses one of these according to the input given.
Thanks in advance for any help.
EDIT: Somehow, now it works. I changed nothing from the original code. I will post the new Test class as now it's a little better. Note that the only important change is that I removed
sc.nextLine()
so now it can read an input like 32 C, without the need to write them into two different lines (as I had to do before). What I don't get is why this work. As far as I know, nextInt() will leave a blank space after reading the number, so when nextLine() reads the input it will read the blank space. That's not the case now, I don't know why.
Working test class:
import java.util.*;
public class TemperaturaTest {
static Scanner sc = new Scanner (System.in);
public static void main (String args []){
System.out.println("Inserisci i valori di temperatura e la scala (C o F).");
int gradi = sc.nextInt();
String scala = sc.next().trim();
Temperatura temp1 = new Temperatura(gradi, scala);
System.out.println(temp1.getCelsius() +" gradi Celsius.");
System.out.println(temp1.getFahrenheit()+ " gradi Fahrenheit.");
}
}