Create A Constructor With Eight Parameters Containing Both String And Integers

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


ADVERTISEMENT

Create Constructor With Parameters And Methods In Same Class?

Feb 28, 2014

The one problem in my book was to create a constructor for different shirt features, which I did and ran successfully. Out of curiosity, I also added other methods to see if it would run if the parameters were different from the constructor. It keeps giving me a constructor error. So, my question is, am I able to create a class that uses a constructor with parameters and other methods without errors? I'm guessing there's no reason to since it would be wasted space since the constructor could do it but was just curious if it's possible.

Is everything from the constructor down (in the class) and Shirt.oneShirt (in the main) just a waste of time?

Here's my example:

public class Shirt//class name.
{
int collarSize;//data field.
int sleeveLength;//data field.
int pocketNumber;//data field
public final static String MATERIAL = "cotton";//final data field for material.
public Shirt(int collarSize, int sleeveLength, int pocketNumber)//start of constructor.
{

[Code]...

View Replies View Related

Constructor With Parameters

Jan 31, 2015

public class TestClass {
public TestClass(String k){System.out.println(k);}
public static void main(String[] args) {
try {
hello();
}
catch(Exception e){System.out.println(e);}

[Code] ....

Explain how to catch block act as constructor with parameter?

View Replies View Related

Stuck On Constructor With Parameters

Mar 19, 2014

How to use a constructor with parameters where the user inputs the information? I'm doing a problem where I create a Delivery class that accepts arguments for the year, delivery number within the year, distance code (1 for short distance, 2 for long), and weight of package. The constructor is supposed to also determine the eight digit delivery number (combining the year and delivery number, like 20140054 for this year, package #54).

I know I'm not close to being done but I'm struck on the application with the constructor parameters. If I'm asking the user to input the information, does that mean I have to create a no argument constructor so it will compile? Right now it won't compile because it's asking for the parameters but I can't put them.

This is the class:

public class Delivery {
int year;
int delNum;
double weight;
int code;

[Code] .....

And the error is:

CreateDelivery.java:22: error: constructor Delivery in class Delivery cannot be applied to given types;
Delivery firstDelivery = new Delivery();
^
required: int,int,int,double
found: no arguments
reason: actual and formal argument lists differ in length
1 error

View Replies View Related

How To Have Unlimited Object Parameters In Constructor

Nov 21, 2014

I want to create an order system. My Problem is that I want to create it dynamic, with ArrayLists and no fixed Array size.

Basically I have already a User which is able to purchase something.

User a = new User();
Car car = new Car("blue",500);
a.purchase(new Order(new OrderThing(5,car));

This will work!

But I want something like this:

a.purchase(new Order(new orderThing(5,car), new orderThing(2,car), new OrderThing(1,car),...)

Basically I don't know how much OrderThings I want to create before I type in the OrderThings with new Order().

But in java it is before you can construct orderThings you must already know in the constructer how much objects you do want.

Now as im writing this question I got a idea of waiting for an Array of new OrderThing but it don't work. When I write

a.purchase(new Order(new orderThing(5,car), new orderThing(2,car)))

it wants a constructor which is based on Order(orderThing,orderThing)

View Replies View Related

Web Services :: Constructor Parameters Wrong Order In Wsdl2java

Feb 5, 2015

I have generated wsdl2java code using axis 1.4 . One classes has 2 constructor methods (one default constructor and other one has 2 parameters). Other classes that inherit from one class have the constructor parameters in the wrong order. The schema files is correct as they come from the OTA specification. What should be done to get rid of this ordering problem in constructor

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

This is the question:

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

printStrings("abc", 5);

will print the following output: abcabcabcabcabc

This is what I attempted:

public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}

View Replies View Related

Create A Method Called MirrorImage Which Takes Two Integer Arrays As Input Parameters

Jan 8, 2009

Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.

Examples:

data1:{1,2,3}
data2:{3,2,1}
==> true

[code].....

I'm pointing a place outside of the array or something

runtime error (line 8, column 0): java.lang.ArrayIndexOutOfBoundsException: 10

-> array.ArrayQuestions.mirrorImage()

View Replies View Related

Default Constructor That Create Empty Cart

Feb 25, 2014

Java Code:

package collectionsHw_netid;
import java.util.ArrayList;
public class ShoppingCart {
//field items
static ArrayList <String> food = new ArrayList<String>();
static ArrayList <Double> price = new ArrayList<Double>();

[Code] ....

Create a new class, ShoppingCart, with a single field, items, that can hold any number of items (Hint: use an ArrayList)

Create a default constructor that creates an empty cart

Create the following methods. After you write each method, test it by running main.

insertItem which adds an Item to the end of the cart (use only 1 parameter)

print which prints the name and price of each item on a separate line using a loop

removeItem which removes an item at a given index

View Replies View Related

How To Create Object To Be Null If Class Constructor Parameter Is Int

Mar 8, 2015

I have a class of Date with a constructor with 3 parameters in it. Those 3 parameters are int data type just to enter month, year, day.

I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor.

I was asked to call the Date parameter is null, call the default constructor but I thought for the Date parameter I could only enter something like 0,0,0 instead of typing in null, null, null because null is for String data type isn't it?

View Replies View Related

Create A New Instance Carte Of Object Carti Using Constructor

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

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 View Related

Declaring Parameters (fields As Protected String) In Java Class

May 19, 2014

I am trying to declare fields as protected String custom.field.1096; in my java class but it does not allow me. Can I not declare the field as above? Is there any workaround to achieve this?

View Replies View Related

Calculate Sum Of Integers Mentioned In String

Mar 3, 2015

So this I need to write a program which asks the user to enter any string, but the string has to have integers inside. I need to calculate the sum of those integers mentioned in the String. Also return 0 if there aren't any.

User input: I have a 91
Output: 10

I cant seem to find a way scan through the string to find the integers and add them.

String x1=input.next();
char digit= x1.charAt(3);}
public static int findDigitSum (String x)?

View Replies View Related

Two Dimensional Array - Converting Integers To String

Apr 10, 2015

I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):

static String [][] students = new String [14][4];
static int number = 0;
String fName, lName, fullName;
int test1, test2, test3, test4;
String a, b, c, d;

[Code] .....

View Replies View Related

Generating Random String Of Integers - No Repeating Numbers In Java

Nov 9, 2014

I don't want to use an array. I do not know of a function built into Java to allow me to do this.

View Replies View Related

String Formatting - Enter Three Positive Integers Separated By Spaces

Sep 18, 2014

double a = scan.nextInt();
double b = scan.nextInt();
double c = scan.nextInt();

//**********************************Equations**********************************
System.out.println ();

double sum = a + b + c;
System.out.printf("Sum = %d", sum);

Heres the error I'm getting

Enter three positive integers separated by spaces, then press enter:
15 20 9

Sum = Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at project2.main(project2.java:52)

View Replies View Related

Null Detected In String Array Not Saved By Class Constructor

Mar 12, 2014

I've a vertical-bar-delimited file where most elements contain text, some contain whitespace, and some are empty. Examples:

62RG|fe|Pencil Financial Group, LLC||doug@pencil.com|||85637889|Cross, Ben|bcross@godaddy.net|Bernard|Cross|Ben||315 One Tree Hill Terrace|Lafayette|LA

62RG|fe|Pencil Financial Group, LLC||tracy@pencil.com|||13658734|Dustin Cardwright|dcart@motorola.com|Dustin|Cartwright|||| |LA

which I parse and store with

String str_arry = innline.split( "|", 17);
lisst.add( new Contact( str_arry));

and my Contact class has the constructor

public Contact( String[] str_arry) {
for( int ii = 0 ; ii < str_arry.length ; ii++ ) {
if( str_arry[ii].matches("^s+$")) {
str_arry[ii] = null;
System.out.println("hit a null");

[Code]...

I expect the for-loop in the constructor to find any elements containing whitespace characters and set them to null for subsequent assignment.And when the code runs I do see some hit-statements pop up, so the detecting part is working.

But when I then process the list and access a Contact object and test fields for nulls I don't find any ie

if( aContactObj.getfFCity() == null) System.out.println("city is null");

never prints when it should.

What's the trick? Or is my approach wrong and if so what should it be?

View Replies View Related

How To Call EnrolStudent Method On Unit Constructor In Another Class When Create A New Unit

Mar 29, 2014

public Unit(String code, String name)
{
enrolStudent(student);
this.unitCode = code;
this.unitName = name;
}
public void enrolStudent(Student newStudent){
students = new ArrayList<Student>();
newStudent = new Student(24662496, "Kingsley", " Iwunze");
students.add(newStudent);
}

how can I call this enrolStudent() method on this Unit constructor in another class when I create a new Unit. all I need is to enroll students in units when units are created. below is my create unit method.

public void createUnits( ){
units = new ArrayList<Unit>();
units.add(new Unit("FIT2034", "Java Programming 2"));
units.add(new Unit("FIT2024", "Software Engineering"));
units.add(new Unit("MAT1830","Discrete Maths"));
unit.enrolStudent(new Student(25486321, "Julia", "Garcia"));
unit.enrolStudent(new Student(44589736, "James", "Olivia"));
unit.enrolStudent(new Student(47852103, "Lucky", "Thyriod"));
}

View Replies View Related

Create New Scanner With The Specified String Object

Jul 4, 2014

//Scanner Test
String stream2 = "ab34ef
56";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(stream1);
// find a string "World"

[Code] ....

Matched expression found by findInLine: 34

ef56

is new line? right?

The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end.

then 56 is in new line and it must return ef.What is the problem?

View Replies View Related

Create Binary Tree From A String Of Letters

Jun 15, 2014

Here is the problem:

Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that this will not be a search tree; there’s no quick way to find a given node. You may end up with something like this:

It also says all Letters must be Leaves

Now I had it almost similar to that picture, but it wasn't right. So ive been working on it but im getting some very strange (and frustrating) output from the following methods.

Ive included the display method just for reference. The book told me to use it so I haven't edited it. I believe my main issue is with my (incomplete) insert() method. The output goes into an infinite loop despite having a return statement break the while loop when a character is inserted.

The way I see to solve the problem is just add a (+) whenever a new subtree needs to be created. Say I add A and B, then it first creates a subtree at the root with a (+) and afterwards lists A and B as its leaves. If I insert a C, it should be able to simply move to the right child of the root and deposit the C there.

package pkg4333_hwk1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class Main {

[Code] ....

View Replies View Related

Create A Java Program That Accepts A String

Feb 19, 2014

So basically I have to create a java program that accepts a string and does the following:

1.) Insert a blank space before capital letters
2.) Convert any capital letters into lower case except the first letter(if it is capital).

So if the user enters "HiMyNameIsJohn", the result should be "Hi my name is john". I am having problems inside my changeMe method. Everything works fine including putting a blank space in between upper case words. So from the previous example, my code would show "Hi My Name Is John". I am having issues making the code to convert the upper case letters into lower case. Im pretty sure the code should be right after the str.insert(i, " "). But I do not how to code it. Ive thought about Character.toUpperCase(ch); but doesnt work.

Java Code:

import java.util.*;
public class WordSeparator
{
public static void main(String[] args)
{
String word1, word2;
Scanner userInput = new Scanner(System.in);

[code]....

View Replies View Related

Create Separate String Objects As Strings Are Immutable

Jan 28, 2015

I want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?

while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}

View Replies View Related

Create Dynamic String Array If Don't Know Number Of Strings In The Beginning?

Feb 21, 2014

how to create dynamic string array if we dont know number of strings in the beginning?

View Replies View Related

EJB / EE :: Create Stateless Bean That Instead Of Returning A String Returns Object

Jul 31, 2014

I'm training myself with the EJB 3 technology. I would like to create a stateless bean that instead of returning a String, it returns an object. I tried in the same way I did with the first exercise, but I'm getting several errors.

View Replies View Related

Create Class Employee Which Contains A String Variable Employee Number

Jul 13, 2014

I have searched for totalPay is always 0 and the responses are not related to my problem (that I can tell).This is a class assignment and I have other questions besides why the method is not working.

Here are the instructions: Create class Employee which contains a String variable Employee Number, a String variable Employee Name, an integer Hours Worked, and a double Pay Rate. Create get and set methods for each variable. Provide method "totalPay" which calculates and returns the total pay by multiplying the hours worked by pay rate. Use figure 8.12 as an example of an object with get and set methods.

Unlike other examples of this type question online this one has Employee Number as a String variable. The book is How to Program Early Objects by Deitel 9th edition.We have instructions to do only what she has told us too. At this point it is objects and getter and setter methods. It is only the beginning of week 2.

ON THE LINES WHERE I HAVE QUESTIONS I AM COMMENTING IN ALL CAPS. NOT SHOUTING, JUST TRYING TO MAKE IT EASIER

package test11;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[code]..

Essentially I cannot figure out how to get the method getTotalPay to be called by the Scanner and do the math. I also have this random bracket problem that showed up about an hour ago.

The concept I don't get is that if I omit this.employeeNumber =id; etc. I error all over the place. If I change it to this.employeeeNumber = employee Number Netbeans tells me it is assigning itself to itself, which I know. When I try to use id instead of employeeNumber I get errors all over. If i remove "this" from it the same happens. why in public class employee I have to change to those values (id, name, etc) in the construct and set them again? I don't see where or how they can be used, so why do i have to do it?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved