Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

writing an algorithm in the class that doesn’t contain the main method

$
0
0
My program runs fine but I was wondering if I could somehow change the algorithm (aIncome = aRevenue – aCost) from the main (testClass.java) to the aClass.java. I think I will have to work with Income, Revenue and Cost instead of aIncome, aRevenue and aCost but my main issue is figuring out where to put them (I was unsuccessful putting it in the constructor). Thanks you guys for your help ahead of time.

import javax.swing.JOptionPane;

public class testClass {
	public static void main(String[] args) {
		
		String input = JOptionPane.showInputDialog("Please enter the Revenue.");
		double aRevenue = Double.parseDouble(input);
		
		String input1 = JOptionPane.showInputDialog("Please enter the Cost.");
		double aCost = Double.parseDouble(input1);
		
		
		double aIncome = aRevenue - aCost;
			
		aClass aClassObject = new aClass(aRevenue, aCost, aIncome);
		System.out.println(aClassObject.toString(aClassObject));
	}

}



public class aClass {

		 private double income;
		 private double revenue;
		 private double cost; 

		public aClass(double aIncome, double aRevenue, double aCost)
		 {
			 revenue = aRevenue;
			 cost = aCost;
			 income = aIncome; 
			
		 }
		
		 public String toString (aClass aClassObject){
			 String answer ="The income should be " + income + " the cost is " + cost +  " therefore the revenue is " + revenue ;
			 
			 return answer; 
		 }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>