Create Instance Of Array Of Several Integers And Prints Data Based On Methods
Apr 15, 2014
I have to make two classes. The first one crates an instance of an array of several integers and prints data (average, greatest, lowest, et cetera) based on the second class, which contains the methods. I'm having a problem with the syntax for the first class required to use the methods.
Here's a shortened version of what I have right now just based on processing the number of integers in the array (because if I can get just one method properly connected, I could figure out everything else).
Driver
import java.util.Arrays;
public class ArrayMethodsDriver
{
//Creates the ArrayMethods object
public static void main(String[] args)
{
int[] a = {7,8,8,3,4,9,8,7};
[Code] ....
When I try to compile this, I currently get the "class expected" error on the count part.
View Replies
ADVERTISEMENT
Mar 15, 2014
So in the code below I create an instance of my own triangle class and use one of its methods. The thing is I use one of my triangle classes methods in a method other the main method of my main program so I'm thinking it can't access it?
Any way here's the code for my triangle class
import java.util.Scanner;
public class QudratullahMommandi_Triangle_06 {
Scanner keyboard = new Scanner(System.in);
private double side1;
private double side2;
private double side3;
[Code] ....
and here's the error message
QudratullahMommandi_S_06.java:46: error: cannot find symbol
{ triangle1.outPut();
^
symbol: variable triangle1
location: class QudratullahMommandi_S_06
1 error
View Replies
View Related
Jan 31, 2015
I'm trying to create a circular array which prints out 8 numbers that increase by one and don't exceed 9. If they do, the remaining numbers are printed from 0 on-wards. I have code below which does this job, but it doesn't really use an array to loop back.
Java Code:
package Practice;
public class Practice
{
public static void main(String[] test)
{
number(7);
number(9);
[code]...
View Replies
View Related
Jan 9, 2015
Write a program (TwoIntegers.java) that prompts the user to enter two positive integers and prints their sum (by addition), product (by multiplication), difference (by subtraction), quotient (by division), and remainder (by modulation). When the user enters 5 and 3, the output from your program should look exactly like the following:
Enter two positive integers: 5 3
The sum is 8.
The product is 15.
The difference is 2.
The quotient is 1.
The remainder is 2.
import java.util.Scanner;
public class TwoIntegers {
public static void main(String[] args) {
System.out.println("Enter two positive integers: ");
Scanner userInput = new Scanner("System.in");
int integer1 = userInput.nextInt();
int integer2 = userInput.nextInt();
[code]....
View Replies
View Related
Feb 6, 2015
How do I pass the data within an initialized array from inside one method to another method of the same class? Will I need to return the array, assigning it to a temp array, which will then be passed as an argument to the other array? The idea is to create an array for an entire year, and be able to manipulate or edit data for a particular month using the other method.
public class Temperature {
static Scanner input = new Scanner(System.in);
static String [] monthArray = {"January", "February", "March", "April", "May",
"June", "July", "August", "October", "November", "December"};
public static void main(String[] args) {
[Code] .....
View Replies
View Related
Feb 8, 2014
I was told to create a program that prints the time between 00:00 (0:00 a.m.) and 23:45 (11:45 p.m.) in the 24-hour clock and 12-hour clock format like this:
24-hour Clock 12-hour Clock
-----------------------------
00:00 0:00 a.m.
00:15 0:15 a.m.
00:30 0:30 a.m.
00:45 0:45 a.m.
01:00 1:00 a.m.
01:15 1:15 a.m.
01:30 1:30 a.m.
01:45 1:45 a.m.
02:00 2:00 a.m.
ect...
but cant seem to create the program and my program doesnt seem to run.
View Replies
View Related
Oct 24, 2014
I have the following 2 classes:
class Address {
private int a;
public void set_a(int a) {
this.a = a;
}
}
class Person {
private Address address;
}
How do i access the method set_a() (through the "address" in Person) from another class which contains main() ?
View Replies
View Related
Jan 8, 2014
Do inherited methods use their instance variables or do they use the ones in the method that inherits them?
For example, Class B extends Class A. Class A and B both have the instance variable "potato". A client program tries to use method "cut" using an object of Class B, but class B has no cut method. So, class B uses the "cut" method inherited from class A. What I want to know is will that cut class A's potato or class B's?
View Replies
View Related
Jun 30, 2014
so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...
for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.
i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?
in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?
Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
[code]...
View Replies
View Related
May 5, 2014
Okay, so I have to create a class with instance data and instance methods.
First, BankAccount class. It should contain the following information, stored in instance variables:
First name: The customer's first name.
Last name: The customer's last name
Balance: The amount of money the customer has in the account.
It should have the following methods:
BankAccount(String firstName, String lastName,
double openingBalance)
This constructor creates a new BankAccount
public String firstName()
Returns the customer's first name
public String lastName()
Returns the customer's last name
public double balance()
Returns the customer's account balance
Finally I need to create a driver to test my class. And create several accounts and verify that the first name, last name, and balance methods work properly. This is my code below.. I don't know if I did it right.
public class BankAccount {
String firstName, lastName;
double balance;
public BankAccount(String firstName, String lastName, double balance) {
[Code] .....
View Replies
View Related
Feb 19, 2011
I thought static methods could never use instance variables, because they wouldn't know which instance to look at.
From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."
Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?
View Replies
View Related
Sep 18, 2014
.I was reading head first java book and saw a barbell question on page no. 280,question-"what if you want to write a class in such a way that only one instance of it can be created,and anyone who wants to use an instance of the class will always use that one,single instance?"
View Replies
View Related
Sep 25, 2013
Can I declare a class as
public static final?
Because I can declare a variable as public static final pi=3.14;
View Replies
View Related
Jun 15, 2014
Check out the following basic code (assume that dog has a method called bark):
Dog d = new Dog();
Object o = d;
o.bark(); // error
But why? Isn't o just a pointer to a memory address which has the dog object? If so, why can't the compiler see that the object has a method called bark? Or, to ask the question another way, why is Java designed to check the object reference to see if the method exists instead of the object itself?
View Replies
View Related
Sep 26, 2014
I want to create a new instance of a Java model class for each new request coming to a servlet.
How to do that without doing that in doGet() or doPost().
View Replies
View Related
Jan 20, 2015
The second message dialog result is always 0 ... i want to find the minimum grade...
import javax.swing.*;
import java.util.Arrays;
public class Parrarrapapa{
public static void main (String[]args){
String length = JOptionPane.showInputDialog("Number of students");
[code]....
View Replies
View Related
Nov 16, 2014
Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:
int[] Scores = new int[1000];
//code to ask user for input and store into array //
The user only inputs lets say 5 scores out of possible 1000. I then try to print it out by doing something like this:
for(int counter = 0; counter < Scores.length; ++counter) {
System.out.println(Scores[counter])
}
How would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.
View Replies
View Related
Oct 26, 2014
I can't figure out where to create the StringHandler object. My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).
If I do like this, null is also sent to Utskrift. I dont want that to happen.
If I put StringHandler last in the loop ord can not be resolved.
String text = "";
while (text != null){
text = showInputDialog(null, "Enter text:");
StringHandler ord = new StringHandler(text);
if (text == null){
[Utskrift(ord.getNumber(), ord.getString(), ord.getWords());
break;
}
}
View Replies
View Related
Aug 9, 2014
Essentially, the code I want is:
public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}
ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.
View Replies
View Related
Mar 30, 2014
what have I done wrong n the following code? I'm trying to create a new instance carte of object Carti using the constructor and then to insert a row into a table created with SQL.The error I'm getting is:
Exception in thread "main" java.lang.NullPointerException
at Carti.Carti.InsertCarti(Carti.java:103)
at Main.main(Main.java:37)
Java Result: 1
BUILD SUCCESSFUL (total time: 28 seconds)
The line Main.main(Main.java:37) is when I try to insert the row.
The line Carti.Carti.InsertCarti(Carti.java:103) is when I do the
PreparedStatement st = conn.prepareStatement("insert into Carti (Id,titlu"
+ ", descriere, autor, editie, anPublicare) values (?,?,?,?,?,?)");
Here is the code (Main and Carti Classes)
import Carti.Carti;
import Imprumut_Carti.Imprumut_Carti;
import Membrii.Membrii;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.sql.SQLException;
import java.text.DateFormat;
[code]....
View Replies
View Related
Apr 8, 2015
So I'm trying to write a program that prints out the "most-repeated integer" in an Array.
For example: if an array contains {1,2,2,3} It would print out 2 as the result.This is what I got so far and according to my knowledge I think I'm correct but for some reason it doesn't work.. Please give me some inputs.
public class MostInt{
public MostInt (){
int[] array = {0};
for(int i = 0;i>array.length;i++){
if(i==i++){
System.out.println(i);
[Code]...
View Replies
View Related
Oct 9, 2014
Creating a class. Specification are given as:
Create a class called Employee that includes three pieces of information as instance variables:
-Employee ID (string type)
-first name (string type) (default value 'John')
-last name (string type) (default value 'Smith') and
-monthly salary (type double).
-No argument constructor that initializes the three instance variables. The employee id should be generated using the following process:
The employee id should be a combination of first initial, last initial and a number starting from 10001 for the first employee and increasing by one for each employee. e.g. if John Smith is the first employee then its id will be JS10001 and if George Brown is the second employee then its id will be GB10002
-Provide get and set methods for each instance variable. The set method for monthly salary should ensure that its value remains positive - if an attempt is made to assign a negative value, leave the original value.
View Replies
View Related
Jan 28, 2015
New to Java Swing. What I am trying to achieve here is to create an instance of the class used to populate the DefaultListModel when a JList item is selected. All examples I have seen show how to return the text displayed in the JList which really is of no practical use.
I have achieved what I want to achieve in that I am successfuly creating the selected object within my ListCellRenderer class.
For the purpose of testing I am using a call to the JOptionPane.showMessageDialog method to display the id of the selected object.
So, I launch the JFrame, the JList is populated, I select an item from the JList and the id value is displayed in the prompt. All this works fine except for one thing.
When I click OK on the JOptionPane.showMessageDialog prompt, the prompt disappears and reappears. The system seems to be in a loop. It is behaving as if the change or click event handler on the JList keeps firing.
This happens even without the creation of the object:
BookEntry be = (BookEntry)list.getModel().getElementAt(list.getSelectedIndex());
I have scoured the code and object properties to see if I can figure out where this behaviour is coming from without success.
All there is in the JFrame design view is a single JList with a variable name of "list". Everything else is in code as below:
Here is the .java code. Its not that complex
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.DefaultListModel;
public class NewJFrame3 extends javax.swing.JFrame {
[Code] .....
View Replies
View Related
Feb 28, 2015
I have to create a constructor with eight parameters containing both string and integers.the variables were supposed to be entered by user. but when I try to create an object of the class the IDE post error messages about the constructor.
public class hfiledriver {
//the class name is hfile
//after the main method I try creating an object of the class
//after prompting the user to enter the data
hfile hfile= new hfile(firstname, lastname, gender, age, weight, height);
}
View Replies
View Related
Apr 17, 2015
I need to generate XML file based on XSD template in Java, I can parse the XSD file, but don’t know after parsing, how to generate XML file.
View Replies
View Related
Nov 8, 2014
So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:
public static void displayArray(int[] array)
This is what I have done
public class RandomIntegers {
static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");
[Code] .....
View Replies
View Related