Need help with printing out the percents working with 2 files, class dirt & main
ID: 3729300 • Letter: N
Question
Need help with printing out the percents
working with 2 files, class dirt & main;
package pa4;
public class Dirt {
private double clayVol;
private double sandVol;
//default constructor
public Dirt(){
clayVol=0;
sandVol=0;
}
//parameterized constructor
public Dirt(double c, double s){
clayVol = c;
sandVol = s;
}
//"getter"
public double getclayVol(){
return clayVol;
}
public double getsandVol(){
return sandVol;
}
//"setter"
public void setclayVol(double c){
clayVol = c;
}
public void setsandVol(double s){
sandVol = s;
}
//total volume
public double totalDirtVol(){
double totalDirtVol = sandVol + clayVol;
return totalDirtVol;
}
public double clayPercent(){
double totalDirtVol = sandVol + clayVol;
double clayPercent = (clayVol/ totalDirtVol) *100;
return clayPercent;
}
public double sandPercent(){
double totalDirtVol = sandVol + clayVol;
double sandPercent = (sandVol/ totalDirtVol)*100;
return sandPercent;
}
}
package pa4;
import java.util.Scanner;
public class Pa4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// ask user for number of elements
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of dirt samples(#>1):");
int numElements = input.nextInt();
//array of dirt types
Dirt array[] = new Dirt[numElements];
double sandVol;
double clayVol;
if( numElements < 2){
System.out.print("Minimum dirt sample is 2");
return;
}
for(int i=0; i <numElements;i++){
System.out.print("Enter Sample "+(i +1)+" clay volume: ");
clayVol=input.nextDouble();
System.out.print("Enter sample "+(i+1)+" sand volume: ");
sandVol= input.nextDouble();
array[i]= new Dirt(clayVol, sandVol);
}
System.out.print(" Volume Table ");
System.out.print("Sample Clay Sand Total ");
for(int i=0; i <numElements;i++){
System.out.printf("%d %.2f %.2f %.2f ",(i+1),array[i].getclayVol(),array[i].getsandVol(),(array[i].getclayVol()+array[i].getsandVol()));
}
}
}
Explanation / Answer
Dirt.java
public class Dirt {
private double clayVol;
private double sandVol;
//default constructor
public Dirt(){
clayVol=0;
sandVol=0;
}
//parameterized constructor
public Dirt(double c, double s){
clayVol = c;
sandVol = s;
}
//"getter"
public double getclayVol(){
return clayVol;
}
public double getsandVol(){
return sandVol;
}
//"setter"
public void setclayVol(double c){
clayVol = c;
}
public void setsandVol(double s){
sandVol = s;
}
}
_________________
Pa4.java
import java.util.Scanner;
public class Pa4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// ask user for number of elements
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of dirt samples(#>1):");
int numElements = input.nextInt();
//array of dirt types
Dirt array[] = new Dirt[numElements];
double sandVol;
double clayVol;
if( numElements < 2){
System.out.print("Minimum dirt sample is 2");
return;
}
for(int i=0; i <numElements;i++){
System.out.print("Enter Sample "+(i +1)+" clay volume: ");
clayVol=input.nextDouble();
System.out.print("Enter sample "+(i+1)+" sand volume: ");
sandVol= input.nextDouble();
array[i]= new Dirt(clayVol, sandVol);
}
System.out.print(" Volume Table ");
System.out.print("Sample Clay Sand Total ");
for(int i=0; i <numElements;i++){
System.out.printf("%d %.2f %.2f %.2f ",(i+1),array[i].getclayVol(),array[i].getsandVol(),(array[i].getclayVol()+array[i].getsandVol()));
}
double totSandVol=0.0,totClayVol=0.0,tot=0.0;
for(int i=0;i<array.length;i++)
{
totClayVol+=array[i].getclayVol();
totSandVol+=array[i].getsandVol();
tot+=array[i].getclayVol()+array[i].getsandVol();
}
System.out.printf(" %.2f Percent Sand ",(totSandVol/tot)*100);
System.out.printf("%.2f Percent Clay ",(totClayVol/tot)*100);
}
}
___________________
Output:
Enter the number of dirt samples(#>1):5
Enter Sample 1 clay volume: 1.2
Enter sample 1 sand volume: 3.1
Enter Sample 2 clay volume: 5.6
Enter sample 2 sand volume: 6.2
Enter Sample 3 clay volume: 8.1
Enter sample 3 sand volume: 6.7
Enter Sample 4 clay volume: 2.4
Enter sample 4 sand volume: 5.1
Enter Sample 5 clay volume: 3.6
Enter sample 5 sand volume: 4.1
Volume Table
Sample Clay Sand Total
1 1.20 3.10 4.30
2 5.60 6.20 11.80
3 8.10 6.70 14.80
4 2.40 5.10 7.50
5 3.60 4.10 7.70
54.66 Percent Sand
45.34 Percent Clay
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.