Download oops_Java Programs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Q4.
A car’s miles-per-gallon (MPG) can be calculate with the following formula;
MPG = Miles driven / Gallons of gas used.
package Chetaw_package;
import java.util.Scanner;
public class Q_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int MPG=0,MD=0,Gas=0;
Scanner a= new Scanner(System.in);
System.out.printf("Number of Miles per Driven is: ");
MD=a.nextInt();
System.out.printf("Gallon of gas used is: ");
Gas=a.nextInt();
MPG=MD/Gas;
System.out.printf("MPG is: " +MPG);
}
}
Q5.
Write a program that asks the user to enter three test scores. The program should display
each Test score, as well as the average of the scores.
package Chetaw_package;
import java.util.Scanner;
public class Q_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
float add=0,over1=0,over2=0,over3=0,average=0;
Scanner a= new Scanner(System.in);
System.out.printf("First over score is: ");
over1=a.nextFloat();
System.out.printf("2nd over score is: ");
over2=a.nextFloat();
System.out.printf("3rd over score is: ");
over3=a.nextFloat();
add=over1+over2+over3;
average=add/3;
System.out.printf("The Average is: " +average);
}
}
Q2.
If one of an operator’s operand is a float, the value of the other operand will be converted
in a double the result of the expression will be a float. For example in the following statement
assumes that x is a short and c is a float:
z = x * y;
package Chetaw_package;
public class Q_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
float x=3;
double y=5;
float c=0,d;
c=(float)y;
d=x*c;
System.out.println(+d);
}
}
Q1.
If one of an operator’s operand is a double , the value of the other operand will be
converted in a double the result of the expression will be a double . for example in the following
statement assume that b is a double and c is an int:
a = b + c;
package Chetaw_package;
import java.util.Scanner;
class Q_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a=1,b=2,c;
int d=0;
c=a+b;
d=(int)c;
System.out.println(+d);
}
}
Q3.
If one of an operator’s operand is a long, the value of the other operand will be converted
in a long the result of the expression will be a long. For example in the following statement
assumes that a long is a long and b is a short:
c = a - b;
package Chetaw_package;
public class Q_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long a=3;
float b=4;
double c,d;
d=(double)b;
c=a-b;
System.out.println(+c);
}
}
Related documents