Java Interface As Argument

Nov 19, 2014

I have 2 files which are as follows:

ICharacterReader.java::

import java.io.EOFException;
public interface ICharacterReader {
char GetNextChar() throws EOFException;
void Dispose();
}

And another file:

SimpleCharacterReader.java::
import java.io.EOFException;
import java.util.Random;
public class SimpleCharacterReader implements ICharacterReader {
private int m_Pos = 0;

[Code] ....

My task is as follows:

1.) Write a class that takes an ICharacterReader interface as an argument and returns a list of word frequencies ordered by word count and then alphabetically.

And also write a main method of a console application that exercises this class using a SimpleCharacterReader, and prints the output to the console.

For example, if the stream returns "It was the best of times, it was the worst of times" then the output will be:

it - 2;
of - 2;
the - 2;
times -2;
was - 2;
best - 1;
worst - 1;

2.) Test the answers in part 1, by writing unit test cases.

How to pass interface as an argument and what is meant by "writing unit test case"?

View Replies


ADVERTISEMENT

Passing Object Argument To A Method - Unable To Call Methods On Argument

Feb 7, 2015

I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).

I don't know if I explained right, so here's a line thing of how the classes relate:

Search >>>(field.getText())>>> displayRecord(Product p) >>>editProduct(p)>>> EditProduct

And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method.

Java Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;

[Code] ....

I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.

View Replies View Related

Read Property File From Command Line Argument In Java

May 8, 2015

I want to read property file in java from command line argument,i.e. if i have some parameters which i have to use separately , i comment the parameters which i don't have to use that time , then uncomment when i have to use.

Is there and way to read the required parameters through command line arguments in java.

View Replies View Related

How To Search Array Elements Present Or Not Using Command Line Argument In Java

Mar 15, 2014

how to search array elements present or not using command line argument in java

View Replies View Related

Read In Java Source Code File As Command Line Argument

Oct 31, 2014

I am trying to complete this question. I understand the most of it but I haven't go a clue to read in the file name.

Full question: Implement a program that reads in a Java source code file and checks to see if it has balanced {}brackets. Your program should use a stack, implemented as a linked list, to check the brackets.

NOTE: you can use a reference called top which points to the head of the list. Your program should run as a command line program and should take a filename as an argument and print one of BALANCED or NOT BALANCED.

For example: c:> java checkBalanced "myProgram.java" BALANCED

View Replies View Related

Interface Concept In Java

Aug 23, 2014

I am having a hard time trying to understand interface concept.

-what is an interface?
-What the use of an interface?
-what does implement or to implement means?
-What does implementation means?

View Replies View Related

Interface And Abstract Classes In Java?

Jun 4, 2014

why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.

View Replies View Related

Java Servlet Interface Is Expected Here

Dec 15, 2014

I am absolutely new to Java. I am creating a Servlet and getting the error interface is expected here.

I am using IntelliJ 14.

My Servlet code is as follows:-

package ERPdetector;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class ErpServlet implements HttpServlet {

[Code] ....

View Replies View Related

Comparing 100 Objects Using Comparator Interface In Java?

Jul 4, 2014

Is it possible to compare 100 objects using comparator interface in java?

View Replies View Related

Java Interface - Implement Method In Another Class

Sep 17, 2014

So I created an interface which has the method

int getCalls();

I am trying to implement this method in another class but I'm not sure how to do so. My attempt is:

public getCalls(){ return getCalls(); }

When I run the program it sends the following exception:

Exception in thread "main" java.lang.StackOverflowError
at FibonacciForget.getCalls(FibonacciForget.java:14)
and it highlights the [return getCalls();] part.

What is the correct way to implement the getCalls() method?

View Replies View Related

How To Do Multiple Inheritance Without Single Interface In Java

Apr 3, 2014

how to do multiple inheritance without sing interface in java?

View Replies View Related

Interface Extends More Than One Interface

Jun 9, 2014

Below code gets printed as output?

public interface I1 {
public void method();
}
public interface I2 {
public void method();
}
public interface I3 extends I2, I1 {

[Code] ....

View Replies View Related

Error In Command Line Argument

Mar 8, 2014

class A
{
public static void main(String a[]){
System.out.println(a[0]);
}
}

This is my code.... i got error......

Exception in thread "main" java.lang.NoClassDefFoundError:a(wrong name: A)

View Replies View Related

Passing Array As Cmd Line Argument

Mar 10, 2014

Can we pass array as a cmd line argument as follows ?

>java TestRun file[], number
if yes, in the main(), how do we capture this array ?
public static void main(String [] args){
String [] files = args1 ;
// or will it be String [] files = args1[];
}

View Replies View Related

Parent Class - No Argument Constructors

Mar 10, 2014

class Test3 {
} class MySub extends Test3 {
}
class Test4{
public static void main(String args[]) {
MySub m = new MySub();
}
}

I learned that if a class and its parent class both have no constructors, the compiler is supposed to complain. When I compiled Test4, i got no errors. why did it give no errors?

I read this article : [URL] ....

View Replies View Related

JSF :: Passing Argument Parameters Between Pages

Jul 19, 2014

So I have an application where the user logs in (using j_security_check). User is taken to a welcome page where user's name is displayed as a link. When clicking that link I would like to take the user to a page where the user is able to update the credentials (password, address, etc). In this way the user only has access to the link related to that specific user's credentials. I am trying the following structure:

The welcome page (adminindex.xhtml) is:

<h:form>
Welcome to admin <h:commandLink value="#{userb.loggedUser}" action="#{userb.selectedUser}"/>
<f:param name="userName" value="#{userb.userName}"/>
</h:form>

The user backing bean:

public String selectedUser() {
userName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(userName);
selUser=uServ.findByName(userName);
return "UpdateUser";

[Code] ....

The last line of the stack suggests that a null PK value is being picked up by the FacesContext method in the backing bean. I'm confused because the userName string IS the primary key of the user table which is structured like this:

CREATE TABLE sha_users
( username VARCHAR(255) NOT NULL
, password VARCHAR(255) NULL
, PRIMARY KEY ( username ) );

I'm sure I'm getting the concept of how to pass query parameters...

View Replies View Related

Why Main Method Has Argument String

Oct 6, 2014

One interviewer has asked me one question that why main() method has argument "String[] arg".What is reason behind this ??I am unable to explain it because i never think about it ..

View Replies View Related

Access Integer Value From Command Line Argument?

Apr 3, 2014

I want to access integer value from command Line argument without changing body of main method.?

View Replies View Related

Make Simple Program Which Takes In Argument?

May 12, 2015

So I'm trying to make a simple program which takes in an argument (target) and then looks through an ArrayList of strings. If it finds a string that begins with (target) then it will return the index of that string. If it doesn't find a string which begins with (target) then it will return -1 instead.

For some reason, the program is always returning -1, rather than the index of the string within the ArrayList when there is one which matches the search criteria.

Here is the code:

public int getIndex(ArrayList<String> text, String target)
{
int i = 0;
int index = -1;
boolean found = false;

[Code].....

View Replies View Related

Pass Predefined Size Array As Argument

Jan 9, 2014

Is this possible in Java?

public void doSomething(int[4] year){
}

What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.

What would be the best way to accomplish this?

View Replies View Related

Argument Program - Missing Return Statement

Jan 22, 2015

I am trying to compile the arguments program but it is giving missing return statement error. how to correct the error message.

public class Arguments {
public static Void main ( String args[])
{
int count, i=0;
String sdata;

[code]...

View Replies View Related

Passing A Null Argument To Overload Procedure

May 24, 2015

I am trying to create a class to read data from the JOptionPane. I am trying to create a general procedure and then be able to overload it using the rest. Basically I just want to call the procedure readString with options and without options while verifying that the user entered the correct information.

Java Code:
public class BankIO {
public static final Object[] mainMenu = {"1. Create Account", "2. Delete Account", "3. Update Account", "4. Display Information", "5. Quit"};
public static final String APPLICATION_NAME = "Banking Pro";

[Code].....

View Replies View Related

Reading TXT File As Command Line Argument?

Apr 23, 2015

I'm having trouble with how to read a text file into my program so the words can be sorted alphabetically. Should I use something like a FileReader?

An example of what I'm trying to do is a .txt file that holds the statement "java is a simple object oriented and distributed and interpreted and robust and secure and dynamic."

The output should organize the words like so: and and and and distributed dynamic interpreted is java object oriented robust secure simple

Here's what I have:

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java AscendingAlphabet"

[Code].....

I can never get it to read the file correctly, it just prints the "usage: java..." statement.

View Replies View Related

Method Get In Type Paths Is Not Applicable For Argument

Jun 4, 2014

I am using Oracle Java 8 in Eclipse working on both Ubuntu and OSX. I have this code:

Java Code:

private static String getConfigDir(){
Path configDir = Paths.get(homeDir(), homeConfig(), appName());
return configDir.toString();

[code]...

But for the method get() of Paths, I get this error in eclipse.The method get(String, String[]) in the type Paths is not applicable for the argument..Yet on the Oracle documentation site, it uses a similar example:

Path p5 = Paths.get(System.getProperty("user.home"),"logs", "foo.log");

View Replies View Related

String Type Array As Argument Of Main

Sep 29, 2014

I have learn that every function in java is treated as a normal function including main() except that execution of a program starts here. I tried to overload it.

But I am getting error while doing so via String type array as an argument of main.

class Hello
{
public static void main()
{
System.out.println("Hello");
}
public static void main(String... s)
{
System.out.println("main from Hello");

[Code] .....

View Replies View Related

Unable To Get GPA Of Student As Command Line Argument

Feb 15, 2015

I'm writing a Java program to get the gpa of a student as a command line argument. Then display the class of the degree according to some criteria. Here is my code. But it says "ArrayIndexOutOfBoundsException :0". How can I fix this?

public class Stgpa
{
public static void main(String[] args)
{
double gpa= Double.parseDouble(args[0]);
if(gpa>=3.6)
System.out.println("Class of the degree is: First Class Hons");
else if(3.6>gpa && gpa>=3.4)

[Code]...

View Replies View Related







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