7 Replies - 964 Views - Last Post: 18 February 2013 - 10:24 AM Rate Topic: -----

#1 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Get/Set Methods with Scanner input

Posted 18 February 2013 - 06:46 AM

Hello
I am a Java beginner and I have just started to learn Java classes
I am trying how to figure out how to get user input when using get and set methods
I know how to user input using just a program using main (See below)
import.java.util.Scanner

public class DIC{
public static void main(String []args){    
int userInput = 0;
Scanner input = new Scanner(System.in);
userInput - input.nextInt();
}
}



However, I cannot figure out how to get the proper user input using a separate Class and set and get methods.

How can I get the user to enter the values 10 for height and 25 for width
public class Oblong
{
	// instance variables
	private double height;
	private double width;

	// 2 getter methods
	public double getHeight()
	{	
		return height;		
	}

	public double getWidth()
	{	
		return width;		
	}
	//2 Setter Methods
	public void setWidth(double widthIn)
	{	
		width = widthIn;
	}

	public void setHeight(double heightIn)
	{	
		height = heightIn;
	}
	//Calculate Method
	public double calculateArea()
	{	
		return width * height;
	}	

}// end of class

public class OblongTester
{
	public static void main(String [] args)
	{
	
		Oblong myOblong = new Oblong();
		
		myOblong.setHeight(10);
		myOblong.setWidth(25);
		
		System.out.println(myOblong.getHeight());
		System.out.println(myOblong.getWidth());
		System.out.println(myOblong.calculateArea());
	
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Get/Set Methods with Scanner input

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 07:02 AM

Obtaining the user input could be done with a method, either in the Oblong class or in OblongTester. The way you've asked, I think you want it in the Oblong class, though I'm not sure it's appropriate there. Going with the flow, you'd use the code from your first posted code and add it to a method in Oblong:
    // method getUserInput() obtains input from the user and applies
    // it to the width and height variables
    public void getUserInput()
    {
        Scanner input = new Scanner(System.in);

        // code the prompts and assignments here
        // e.g. width = input.nextInt()
        // you should also add input verification
    }

You'd then use your Oblong instance in OblongTester, myOblong, to call the method and assign the height/width values:

myOblong.getUserInput();

This post has been edited by GregBrannon: 18 February 2013 - 07:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 07:31 AM

Cheers Greg. So
// method getUserInput() obtains input from the user and applies
	// it to the width and height variables
	public void getUserInput()
	{
	    Scanner input = new Scanner(System.in);
	 	 System.out.println("Please enter the width");
	    width = input.nextDouble();
		 System.out.println("Please enter the height");
	    height = input.nextDouble();
		 // you should also add input verification
	}



To call this method in the tester class would I use
public class Q2
{
	public static void main(String []args)
	{
		Oblong myOblong2 = new Oblong();
				
		System.out.println(myOblong2.getUserInput());
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 07:43 AM

I'm not meaning to encourage you to continue the practice of having a main() method with the static methods it needs as a typical design pattern, but that is typical for tester classes. Just realize that you're going to move away from this design eventually, hopefully soon.

So to answer your question, to call the getUserInput() method from the tester class, most likely from the main() method, you'd include getUserInput() in the tester class and define it static:

public static void getUserInput() { . . . }

Then the values of width and height would be set by getUserInput():

myOblong.setHeight( input.nextDouble() );
Was This Post Helpful? 1
  • +
  • -

#5 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 08:00 AM

Thanks Greg

I was wondering if you could explain this statement a little more please? I am very interested in what you have said.

"...I'm not meaning to encourage you to continue the practice of having a main() method with the static methods it needs as a typical design pattern, but that is typical for tester classes. Just realize that you're going to move away from this design eventually, hopefully soon."
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 08:28 AM

Your programs will evolve to having main() methods with a few lines and no static methods. The main() method will create an instance of a main controlling class, maybe a few helper classes, then start the main controlling class on the appropriate thread, transferring control to the main controlling class and exiting. Classes built solely for test are understandable exceptions to this goal.
Was This Post Helpful? 1
  • +
  • -

#7 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 336
  • Joined: 28-September 12

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 09:00 AM

import java.io.*;
class Oblong{
     private int height,width;

    public double getHeight()

    {  
       return height;     

    }

    public double getWidth()

    {  
        return width;      
    }

    //2 Setter Methods

    public void setWidth(int widthIn)

    {  
        width = widthIn;
    }

    public void setHeight(int heightIn)

    {  
        height = heightIn;
    }
     public double calculateArea()
    {  
        return width * height;
    }
}

class OblongTester{
public static void main(String args[]){
    int height=0,width=0;
    DataInputStream in=new DataInputStream(System.in);
    try{
        System.out.println("Enter Height:");
        height=Integer.parseInt(in.readLine());
        System.out.println("Enter Width:");
        width=Integer.parseInt(in.readLine());
    }
    catch(Exception e){
        System.out.println("I/O Error");
    }
    Oblong o=new Oblong();
    o.setHeight(height);
    o.setWidth(width);
    System.out.println(o.getHeight());
    System.out.println(o.getWidth());
    System.out.println(o.calculateArea());


}
}

This post has been edited by CY5: 18 February 2013 - 09:01 AM

Was This Post Helpful? 1
  • +
  • -

#8 kevinb4940  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 45
  • Joined: 08-October 12

Re: Get/Set Methods with Scanner input

Posted 18 February 2013 - 10:24 AM

Thanks CY5 for the detailed answer. Thanks to Greg for all your help also

Awesome website.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1