Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Accessor and Mutator Methods

707697Jun 25 2008 — edited Jun 25 2008
I'm currently learning how to use different types of methods within my program.

One of the things that I'm getting stuck on is how to get user input and then feed that into a new instance of the class that I made.

Here is my cat class
public class Cat
{
	private String name;
	private int age;
	private double weight;
	private String breed;
	private boolean declawed;
	private double percentGrowthRate;

	public void setName(String name)
	{
		this.name = name;
	}

	public void setAge(int age)
	{
		this.age = age;
	}

	public void setWeight(double weight)
	{
		this.weight = weight;
	}

	public void setBreed(String breed)
	{
		this.breed = breed;
	}

	public void setDeclawed(boolean declawed)
	{
		this.declawed = declawed;
	}

	public void setDeclawed() // This will get overloaded later
	{
	}

	public void Display()
	{
		System.out.printf("Name: %s\n Age: %d\n Weight: %f\n Breed: %s\n Declawed: %b\n", this.name, this.age,
			this.weight, this.breed, this.declawed);
	}

}
And here is my Driver program
import java.util.Scanner;

public class CatDriver
{
	public static void main(String[] args)
	{
		String catName = "";
		int catAge = 0;
		double catWeight = 0.0;
		String catBreed = "";
		boolean catDeclawed = false;
		Cat cat1, cat2, cat3;

		Scanner stdIn = new Scanner(System.in);

		System.out.print("Enter the name of your cat: ");
		catName = stdIn.nextLine();

		cat1 = new Cat(); // this is the part I'm having trouble with

		System.out.printf("How old is %s: ", catName);
		catAge = stdIn.nextInt();
		cat1.setAge(catAge);

		System.out.printf("How much does %s weigh: ", catName);
		catWeight = stdIn.nextDouble();
		cat1.setWeight(catWeight);

		System.out.printf("What breed of cat is %s: ", catName);
		catBreed = stdIn.nextLine();
		cat1.setBreed(catBreed);

		System.out.printf("Has %s been declawed: ", catName);
		catDeclawed = stdIn.nextBoolean();
		cat1.setDeclawed(catDeclawed);

		cat1.Display();


	} // end main
} // end class
What I tried doing first was saying
Cat catName = new Cat();
But that gave me an error and wouldn't work. So my first question is how do you get input from the user and then name an instance of a class after that input (I don't even know if I'm saying that right but hopefully someone understands ><). I want the user to enter the name Gus and then have it create a new instance of the Cat class called Gus.

My second question is, when the code is compiled and run as is, I have some sort of format error when it gets time to ask me the cat's breed. I get asked for the breed AND the declawed on the same line and never have a chance to put in the breed. I can't see any reason why it would do this so anyone who could clear that up also would be appreciated.

Comments

thatJeffSmith-Oracle

We need more info to help you. What type of chart are you using? What are you setting for series, group, and value?

It does work, I've taken similar data from HR.EMPLOYEES and have a bar chart plotting just fine.

http://www.thatjeffsmith.com/wp-content/uploads/2014/01/otn_chart.png

user12861356

Can you show me the actual query you used to create that?

thatJeffSmith-Oracle

select hiredate, last_name, salary from hr.employees

user12861356

My query

SELECT

  a.RUN_DATE,

  b.PROC_NAME,

  a.SM1_COUNT

FROM OPS_SUMMARY_LOG a

LEFT JOIN OPS_DIM_PROCESS b

ON a.PROC_ID = b.PROC_ID

WHERE a.PROC_ID = 1 AND RUN_DATE    > (SysDate - 30)

GROUP BY a.RUN_DATE, b.PROC_NAME, a.SM1_COUNT

ORDER BY a.RUN_DATE, b.PROC_NAME;

Chart type: Line or Bar Vertical Cluster (I have tried both)

Group: "RUN_DATE"

Series: "PROC_NAME"

Value: "SM1_COUNT"

The preview shows the labels in X axis and the legend with a blue box on the right, background grid line for each date but no actual data. If I change the report type to "Table"it displays the actual rows as expected.

thatJeffSmith-Oracle

is SM1_COUNT a number?

user12861356

Yes, it's data type is "Number"

thatJeffSmith-Oracle

ok, well can you try this with a sample data set like HR.EMPLOYEES? I don't have your data so I can't build your exact report.

user12861356

RUN_DATE is of type DATE and PROC_NAME is of type VARCHAR2(40).

If you have time I can show you my screen. I just need an email to send an invite to.

thatJeffSmith-Oracle

This isn't support, I'll try to help, but you need to answer my questions.

ok, well can you try this with a sample data set like HR.EMPLOYEES?

user12861356

I have created a new table and added 5 records.

02-JAN-14JONES85230
20-DEC-13SMITH92150
11-NOV-13TAYLOR125239
09-OCT-13GREEN75284
13-SEP-12MULLINS248377

The query is this: select * from hr_employees, in the UI Group/Series/Value I have LAST_NAME/HIREDATE/SALARY.

Same result.

user12861356

I get the legends on the right-side as the 5 dates, labels on X axis as the lastnames but no bars. Chart type is Bars

thatJeffSmith-Oracle

save the report to XML and email it to me at jeff.d.smith@oracle.com

user12861356
Answer

You may want to know (or maybe not) how this ended...

I was having this problem while using the InstantClient with SD4. When I changed to use the full-blown Oracle Home 11gR2 it worked. (I have several clients installed in my machine)

I got the hints from here: http://www.thatjeffsmith.com/archive/2014/01/oracle-sql-developer-4-and-the-oracle-client/

Thanks!

Marked as Answer by user12861356 · Sep 27 2020
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 23 2008
Added on Jun 25 2008
16 comments
1,045 views