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

Getting User Input from a Class and Displaying in Main

$
0
0
Hello

I am trying to teach myself Java from books and youtube videos.

Could someone please check the answer to my question below and tell me if I am programming the answer correctly please?

/*2. Write a program which instantiates two separate BankAccount objects. The
program should then ask the user to enter an amount to deposit to each
account. The program should end by printing out details of the BankAccount
which has the highest balance.*/
public class Q2
{
	public static void main(String []args)
	{
		double x, y;
		BankAccount2 accOne = new BankAccount2();
		BankAccount2 accTwo = new BankAccount2();
		
		x = 	accOne.userIn();
		y = 	accTwo.userIn();
		
		System.out.println("Balance one is "+ x );
		System.out.println("Balance one is "+ y );
		
		if (x > y)
		   System.out.println("The Hightest Balance is " + x);
		else
			System.out.println("The Hightest Balance is " + y);
	}	
}



Here is the BankAccount2 class
import java.util.Scanner;

public class BankAccount2
{
	public double userIn()
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter an amount to deposit");
		double deposit = input.nextDouble();
		return deposit;
	}
	
}//end class


Viewing all articles
Browse latest Browse all 51036

Trending Articles



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