OOP with Java 3

OOP with Java 3

Write a Temperature class that has two instances variables: temperature value (a floating-point number) and a character for the scale, wither C for Celsius or F for Fahrenheit. the class should have four constructor methods: one for each instances variable (assume zero degrees if no value is specified and Celsius if no scale is specified). one with two parameters fir the two instance variables, and a no-argument constructor (set to zero degree Celsius). Include the following:

a. Two accessor methods to return the temperature - one to return the degrees Celsius, the other to return the degrees Fahrenheit, use the following formulas to write the two methods

degreeC = 5(degreeF - 32)/9
degreeF = (9(degreeC)/5) + 32

b. Three mutator methods: one to set the value, one to set the scale (F or C). and one to set both.

Then write a driver program that tests all the methods.
View Answers

February 10, 2010 at 3:50 PM

Hi Friend,

Try the following code:

import java.util.*;
class Temperature{
double temp;
char scale;
Temperature(double temp,char scale){
this.temp=temp;
this.scale=scale;
}
Temperature(char scale){
setTemp(0);
this.scale=scale;
}
Temperature(double temp){
this.temp=temp;
setScale('C');
}

public void setTemp(double temp){
temp=temp;
}
public void setScale(char scale){
scale=scale;
}
public double convertToCelsius(){
double c= (temp - 32) / 1.8;
return c;
}
public double convertToFahrenheit(){
double f=(temp*1.8+32);
return f;
}
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter temperature: ");
double t=input.nextDouble();
System.out.print("Enter scale: ");
String s=input.next();
char ch=s.charAt(0);
if((ch=='C')&&(t!=0)){
Temperature t1=new Temperature(t,ch);
double tempF=t1.convertToFahrenheit();
System.out.println(tempF+"F");
}
else if((ch=='F')&&(t!=0)){
Temperature t2=new Temperature(t,ch);
double tempC=t2.convertToCelsius();
System.out.println(tempC+"C");
}
else if((ch!=' ')&&(t==0)){
Temperature t3=new Temperature(ch);
if(ch=='C'){
double tempF=t3.convertToFahrenheit();
System.out.println(tempF+"F");
}
else if(ch=='F'){
double tempC=t3.convertToCelsius();
System.out.println(tempC+"C");
}
}
else if((ch==' ')&&(t!=0)){
Temperature t4=new Temperature(t);
double tempF=t4.convertToFahrenheit();
System.out.println(tempF+"F");
}
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
OOP with Java 3 - Java Beginners
OOP with Java 3  Write a Temperature class that has two instances variables: temperature value (a floating-point number) and a character for the scale, wither C for Celsius or F for Fahrenheit. the class should have four
java oop
java oop  Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day... is Sunday. Similarly is we subtract 3 days, the day to be return is Friday
Advertisements
Java with OOP assignment (Eclipse)
Java with OOP assignment (Eclipse)  How do i control the length of characters in an input? E.g.: Enter your name > Hi * Name too short
OOP - Java Beginners
OOP Design Pattern  What is the oops design patterns
OOP JAVa - Java Interview Questions
OOP JAVa  Is java is 100% OOP.?Plz giv full explanation.  hi Sanjay,dis is Srinivas(MCA from Andhra University).....Java is not an 100 % OOPL,because of the availability of Primitive Data Types
oop answers - Java Beginners
oop answers  Consider the following declarations: { private int.... Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement that creates the XClass object t and initializes
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private... of a and the instance variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java
OOP - Java Beginners
OOP  i have problem with OOP hope u'll help me write a program that allows the user to enter student' names followed by their test scores and outputs...://www.roseindia.net/java/master-java/java-object-oriented-language.shtml
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private int u; private double w; public XClass() { } public XClass(int a, double...; } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java - Java Beginners
OOP with Java  I had a look at the solution for the answers.... Assume a small airplane with seat numberings as follows: 1 A B C D 2 A B C D 3... B C D 2 A X C D 3 A B C D 4 A B X D 5 A B C D 6 A B C D 7 A B C D
java"oop" - Java Beginners
Java OOPs Concept  What is OOPs programming and what it has to do with the Java?  Hi i hope you understand it.//To print the even numbers... between 2 and the given number are"); for(int i=3;i<n;i++) { if(i%2==0
java oop - Java Beginners
java oop  Consider the following declarations: { private int u... variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement
JAVA OOP - Java Beginners
JAVA OOP  Employees in a company are divided into the classes Employee, HourlyPaid, Salescommissioned and Executive for the purpose of calculating their weekly wages or monthly salaries. The data to be maintained for each class
OOP - Java Beginners
OOP  Which one is better to learn object oriented progamming java or c++? thaks, Bet.  Hello, As of my concern both have different scope and use, you can not merge these. As i java person so i will show you java
OOP - Java Beginners
); } For more information on Java visit to : http://www.roseindia.net/java/ http://www.roseindia.net/java/beginners/Construct.shtml Thanks
oop
oop  can you tell me about why java is not 100% object oriented language
OOP using Java - Java Beginners
OOP using Java  Can you write a Java statement that creates the object mysteryClock of the Clock type, and initialize the instance variables hr,min...); } } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java 2 - Java Beginners
OOP with Java 2  Define a class called BogEntry that could be used to store an entry for a Web log. The class should have member variables to store the poster's username, text of entry, and the date of the entry using the Date
OOP with Java-Array - Java Beginners
OOP with Java-Array  Write a program to assign passengers seats... D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7..., the display should look like: 1 X B C D 2 A X C D 3 A B C D 4 A B X D
OOP Using JAVA - Java Beginners
OOP Using JAVA  OBJECT ORIENTED PROGRAMMING USING JAVA (hope guys u will help me please i need your help,thank you so much) Create a Java program...(); oops.OopType(); } } For more information on Java visit to : http
3 queries on java class
3 queries on java class  Hey. I need to do 3 queries on same class ( JAVA) One is to populate the combobox, the another one to populate tje jlist and the last one to use on button to open a file from database (BLOB) Can some
3 Java Programs for Beginners
3 Java Programs for Beginners  Write a program that prompts the user for 2 different integers, then prints out the numbers between the 2 integers (inclusive) and their squares and the sum of their squares. Write a program
Java with OOP assignment (Eclipse) - Java Beginners
Java with OOP assignment (Eclipse)  "THREE Ts GAME" *Description... is made of 3 X 3 grids. The two players, one being assigned with 'O' and the other with 'X', where each if the two players take turns marking the spaces in a 3 X 3
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.16.0 is released. Learn to use oop-lib-jvm version 0.16.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.16.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.16.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.16.1 is released. Learn to use oop-lib-jvm version 0.16.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.16.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.16.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.0 is released. Learn to use oop-lib-jvm version 0.17.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.1 is released. Learn to use oop-lib-jvm version 0.17.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.2 is released. Learn to use oop-lib-jvm version 0.17.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.2 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.2 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.3 is released. Learn to use oop-lib-jvm version 0.17.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.3 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.3 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.4 is released. Learn to use oop-lib-jvm version 0.17.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.4 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.4 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.0 is released. Learn to use oop-lib-jvm version 0.18.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.18.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.1 is released. Learn to use oop-lib-jvm version 0.18.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.18.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.9 is released. Learn to use oop-lib-jvm version 0.20.9 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.9 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.9 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.9 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.4 is released. Learn to use oop-lib-jvm version 0.30.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.4 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.4 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.30.4 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.4 is released. Learn to use oop-lib-jvm version 0.20.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.4 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.4 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.4 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.3 is released. Learn to use oop-lib-jvm version 0.20.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.3 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.3 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.3 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.2 is released. Learn to use oop-lib-jvm version 0.30.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.2 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.30.2 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.1 is released. Learn to use oop-lib-jvm version 0.30.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.1 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.1 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.30.1 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.0 is released. Learn to use oop-lib-jvm version 0.30.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.0 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.30.0 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.2 is released. Learn to use oop-lib-jvm version 0.20.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.2 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.2 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.3 is released. Learn to use oop-lib-jvm version 0.30.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.3 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.3 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.30.3 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.1 is released. Learn to use oop-lib-jvm version 0.20.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.1 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.1 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.1 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.7 is released. Learn to use oop-lib-jvm version 0.20.7 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.7 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.7 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.7 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.0 is released. Learn to use oop-lib-jvm version 0.20.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.0 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.20.0 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.15.2 is released. Learn to use oop-lib-jvm version 0.15.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.15.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.15.2 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.15.2 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.15.0 is released. Learn to use oop-lib-jvm version 0.15.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.15.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.15.0 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.15.0 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.14.11 is released. Learn to use oop-lib-jvm version 0.14.11 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.14.11 in Java projects. Follow the step...; it.unibo.tuprolog - oop-lib-jvm version 0.14.11 java library in your project...; it.unibo.tuprolog - oop-lib-jvm version 0.14.11 library in your project. Step 3: Now
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.19.0 is released. Learn to use oop-lib-jvm version 0.19.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.19.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.19.0 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.19.0 library in your project. Step 3: Now you
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.2 is released. Learn to use oop-lib-jvm version 0.18.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.18.2 java library in your project. ADS...; it.unibo.tuprolog - oop-lib-jvm version 0.18.2 library in your project. Step 3: Now you
Java Query divide frame into 3 rows and 3 columns
Java Query divide frame into 3 rows and 3 columns  how can we divide the frame into 3 rows and 3 columns

Ads