Unable To Call / Test All Of The 5 Sorting Methods At Same Time In Main Class

Dec 19, 2014

For reference I am programming Java in BlueJ. I am fairly new to the language and I am having trouble with sorting.

I am trying to call / test all of the 5 sorting methods (at the same time) in the main class. To be specific, the sorted list has to technically outputted 5 times.

I figured out how to call / test Quicksort:

Sorting.quickSort(friends, 0, friends.length-1);

But the others are not working correctly. Specifically these:

Sorting.mergeSort(friends, 0, friends.length-1);
Sorting.PbubbleSort(friends, 0, friends.length-1);
Sorting.PinsertionSort(friends, 0, friends.length-1);
Sorting.selectionSort(friends, 0, friends.length-1);

For reference, this is the output when it is not sorted:

Smith, John 610-555-7384
Barnes, Sarah215-555-3827
Riley, Mark 733-555-2969
Getz, Laura 663-555-3984
Smith, Larry464-555-3489
Phelps, Frank322-555-2284
Grant, Marsha243-555-2837

This is the output when it is sorted:

Barnes, Sarah215-555-3827
Getz, Laura 663-555-3984
Grant, Marsha243-555-2837
Phelps, Frank322-555-2284
Riley, Mark 733-555-2969
Smith, John 610-555-7384
Smith, Larry464-555-3489

This is the class Sorting, which I should note is all correct:

public class Sorting{
/**
* Swaps to elements in an array. Used by various sorting algorithms.
*
* @param data the array in which the elements are swapped
* @param index1 the index of the first element to be swapped
* @param index2 the index of the second element to be swapped
*/
private static <T extends Comparable<? super T>> void swap(T[] data,
int index1, int index2){
T temp = data[index1];
data[index1] = data[index2];

[Code]...

This is the Main class in which I am supposed to call the sorting methods, SortPhoneList:

public class SortPhoneList{
/**
* Creates an array of Contact objects, sorts them, then prints
* them.
*/
public static void main (String[] args){
Contact[] friends = new Contact[7];
friends[0] = new Contact ("John", "Smith", "610-555-7384");
friends[1] = new Contact ("Sarah", "Barnes", "215-555-3827");

[Code]...

View Replies


ADVERTISEMENT

Java Swing Frame Can't Call On Other Methods From Main

Mar 25, 2015

I am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:

import javax.swing.*;
import java.awt.*;
 public class BelishaBeacon {
  public void BeaconFrame() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();

[code]....

View Replies View Related

Methods From Original Class Receiving Error When In Test Class

Jul 5, 2014

I am working on a program that simulates a bug moving along a horizontal line, My code works correctly when I test it in it's own class but when I tried testing my constructor and methods in a test class I received an error saying, "package stinkBug does not exist" on lines with my methods. However, stinkbug is not a package.

Java Code:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

[code]....

View Replies View Related

How To Call A Class Within The Main

Apr 17, 2014

How do I call a class within the main. For example, this is what i have in my code right now. I am trying to call the class BlackJackGame.

package blackjack;
public class BlackJack {
public BlackJack() {
BlackJackGame();
}
}

View Replies View Related

How To Write Driver Class To Test Methods

Apr 13, 2015

I am to create a Array class then create a Driver class (TestArray) to test all the methods in the Array Class. Here's the code i've written for the Array Class. I just nee developing the TestArray class.

import java.util.Scanner;
public class Array
{
Scanner sc = new Scanner(System.in);
private double[] array = new double[];
public void setArray(double[] arr) {

[Code] ...

View Replies View Related

Create Two Instances Of Class And Test Each Of Methods

Nov 1, 2014

How do I use two constructors and I'm having trouble with using char for gender...

Write a program to test the Person class defined below. Your test program should create two instances of the class (each to test a different constructor) and test each of the methods. You are also required to illustrate the error in trying to access private data members from the client class (for clarity temporarily change the private modifier to public and test again). See screenshots below for sample output.

The screen shots are displayed as:

p1 name = Not Given Age = 0 Gender = U
p2 name = Jane Doe Age = 0 Gender = F
p1 name = John Doe Age = 25 Gender = M

and

PersonTester.jave:20: name has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 PersonTester.jave:20: age has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
  PersonTester.jave:20: gender has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 
3 errors

Here is the class given :

class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
 
[Code] .....

View Replies View Related

How To Call String From Main Method In Different Class

Dec 9, 2014

The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below. How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I get the other class to initiate the code below which is loacted in my main class.
 
public static String escapeDN(String name) {
  StringBuilder sb = new StringBuilder();
  // space or # character at the beginning of a string
  if ((name.length() > 0) &&
        ((name.charAt(0) == ' ') ||
             (name.charAt(0) == '#'))) {

[Code] .....

View Replies View Related

How To Write A Client Class To Test Methods For Assignment

Jan 20, 2014

Write a class encapsualting the concept of a course grade, assuming a course grade has the following attributes: a course name and a letter grade. Include a constructor, the accessor and mutator, and methods toString and equals.Write a client class to test all the methods in your class.

package labmodule7num57;
import java.util.*;
public class LabModule7Num57 {
// Constructors//

[code]....

View Replies View Related

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

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

public class locker { 
public static void main(String[] args) {
CombinationLock();

[code]....

View Replies View Related

Effect Of Calling Real Service Call In Test?

Jul 17, 2014

I am trying to understand the effect of calling real business logic from the test class. If I have a test class that call the service method which make update to DB. Is DB really changes through test class?

e.g.

Java Code: @Test
public void testUpdate() {
MyDto myDto = new MyDto(paramters to create new myDto object); //creating new dto object
myService.updateMyData(myDto); //passing new dto object for update (DB record really changed??)
MyDto testDto = myRepo.find(myDto.getKey()); //get record for the corresponding key
assert testDto.getSomeProp() == myDto.getSomeProp(); //what testDto.getSomeProp() return? new value send on myDto or old value from DB?
} mh_sh_highlight_all('java');

View Replies View Related

How To Call A Constructor In Main

Jul 3, 2014

I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.

As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.

public class PrepaidCard
{
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
}
public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
}

Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class. Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.

public class PrepaidCardTest
{
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
}

How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?

card2.setCardID(id, token);

View Replies View Related

Placing File In Java Application Accessible For Both Main And Test

Oct 1, 2014

I have style sheet file (e.g. myStyle.xslt). I am trying to decide where to put this file in my web application so that both src and test can access it. All my source in packages inside /src folder and test code in packages inside /test folder.

View Replies View Related

Sorting Runners Into Time Order Shortest First

May 5, 2014

i need to sort these runners into time order shortest first, the classes both work i just cant get the method sortRunnerList() to work

Java Code:

public class Runner implements Comparable <Runner>
{
/* static variables */
private static int nextNumber;
// To be added by students in Question 3, part (i)(a) and part(iv)(a)
/* instance variables */

private int number; // runner's number
private String name; // runner's name

[Code] .....

View Replies View Related

Array Sorting Using Two Different Methods

Nov 18, 2014

The following code is supposed to generate random integers and sort array1 and array2 using two different sorting methods. array1 is to be sorted with a "selection sort" method and array2 is to be sorted with the built-in Arrays.sort() method. However, array1 is the one that has a problem. It does not appear to output any values at all for array size of 4000 or more, such as array1[10000]. The assignment is to generate random integers, sort and benchmark the speeds at which array1 and array2 can generate and sort ints at array1[1000] array2[1000] array1[10000] array2[10000] array1[100000] array2[100000]

/**
*
* The following is a sorting and benchmarking program to sort
* array1 and array2 with 1,000 , 10,000 and 100,000 array sizes.
* array1 uses selection sort from section 7.4 of the book
* and array2 uses the built in Arrays.sort() method.

[code]....

I cannot post the output because the amount of data seems to have crashed the two previous posts I made on this topic due to the size of the problem.

View Replies View Related

Cannot Call Child Method From Main

May 7, 2015

I can call a child method from a main method. If I have a parent called "Assessment", a child called "Quiz", and another child called "test". Can I instinate an object like Assessment a = new test(); and call a method in test.I know the method can be called if I instinate the test t = new test() but that defeats the purpose of inheritance which I'm learning in class.

View Replies View Related

How To Write Test Cases For Void Methods

Mar 19, 2014

I'm trying to write test cases for void methods. Here is my sample code

public void objIntoFile() throws Exception {
addAdminObjects();
file = new File("D:ExtractingDBexport.txt");
writer = new BufferedWriter(new FileWriter(file));

[Code] .....

View Replies View Related

Unable To Call Youtubeviewer From A Window By Actionlistener

Jun 3, 2014

i wanna to call youtubeviewer from a window by actionlistener and i have tried N times....

Java Code:

public class YouTubeViewer {
public YouTubeViewer(){
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("YouTube Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);

[Code]...

note:necessary library for youtubeviewer is DJ Native Swing. if i call directly by main function,it will work.but if i call from actionlistener cant work (maybe crash).

View Replies View Related

Prepaid Card - How To Call A Constructor In Main

Jul 3, 2014

I am trying to call a constructor from PrepaidCard class in my main method, but I am not sure how to proceed.

As seen below, both the PrepaidCard constructor and the setCardID method have the ability to set the card ID.

Java Code:

public class PrepaidCard {
public PrepaidCard(String id, int token) {
cardID = id;
tokenBalance = token;
} public void setCardID(String id, int token) {
cardID = id;
tokenBalance = token;
}
} mh_sh_highlight_all('java');

Now in this block of code, I can successfully pass the id and token value by calling the setCardID method from the PrepaidCard class.

Now I would like to call the PrepaidCard constructor from the PrepaidCard class to pass the id and token value, instead of using the setCardID method.

Java Code:

public class PrepaidCardTest {
public static void main(String[] args) {
PrepaidCard card2 = new PrepaidCard(id, token);
System.out.print("Enter Card2 cardID: ");
id = input.nextLine();
card2.setCardID(id, token);
}
} mh_sh_highlight_all('java');

How to call the PrepaidCard constructor from the PrepaidCard class, to successfully pass the id and token value, in my main method?

Specifically how to modify or replace this line of code so that it can correctly call the PrepaidCard constructor?

Java Code: card2.setCardID(id, token); mh_sh_highlight_all('java');

View Replies View Related

Add New Field And Call The Display Method In Main

Nov 27, 2014

So I have been given this code, below:

public class Person{ 
/*Complete*/
public String format(){
return String.format( "Name: %s", name );

[Code] ....

And I have been asked to add a new field called name, and call the display method in the main. I need to use an appropriate type and privacy for the name field, and once the code is finished, I should see 'Harry Potter' appear on the command line. However I don't know what it means by field. I've tried googling it and one search result said it is a field parameter, and another search result said it was something completely different, and I don't know which one it is. I've created a parameter 'Private String name;' but it only prints out 'name: null'

This is the code I wrote but it obviously doesn't work?

package Practical8;
 
public class Person{
Private String name;
public String format(){
return String.format( "Name: %s", name );

[Code] ....

View Replies View Related

Call Variables From Main To Write To External File

Apr 27, 2015

I have this program, I am wondering if it is possible to call files from the main method and sort them into my saveOneRocord method? If so, how would that look?

my orderProcess Class

package stu.paston.finalprogram;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;

[Code] .....

View Replies View Related

Default Values Passed By JVM In Order To Call Main Method

Aug 27, 2014

For the below program what are the default values passed by the JVM in order to call main() method

class program
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
}
}

View Replies View Related

Represent Time Span Of Hours And Minutes Elapsed - Method Is Undefined For Type TEST

Jul 29, 2014

I am trying to run a class with a client code and I get the following error

The method add(TimeSpan) is undefined for the type TEST

Why i get this error?

package timespan;
// Represents a time span of hours and minutes elapsed.
// Class invariant: minutes < 60
public class TimeSpan {
private int hours;
private int minutes;

[Code] ....

View Replies View Related

How To Call Non Static Methods From Other Classes

Apr 16, 2015

how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??

example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??

class test{
public static void main(String [] args){
Scanner input = new Scanner(System.in); //create new Scanner object
//for input
int number1;
int number2;

[Code]...

View Replies View Related

JavaFX 2.0 :: Unable To Invoke Main Method

Jun 28, 2014

Here is my problem:

Javafx launcher error and beneath this it says

unable to invoke main method

How do I fix this I have a windows 8 hp laptop 64 bit...

View Replies View Related

JSF :: Call Variables Instead Of Methods From A Xhtml Page?

Jun 2, 2014

If I had this code:

<h:dataTable value="#{customer.customerList}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>

And in the server getCustomerList() accessed to database, how many times getCustomerList() would be called from I request the xhtml page?. I have read this would be called several times because of JSF internals and It would be better to store it in a variable and access this variable.

1. Is this true this would be called several times? why?

2. If the previous statement was true, how to avoid it, I mean not call the method from a service?

View Replies View Related







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