Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:40:41+00:00 2026-06-11T17:40:41+00:00

For a Java class we are writing Data structure classes and we need to

  • 0

For a Java class we are writing Data structure classes and we need to write a tester class to go with them. I am going for extra credit and trying to write a single tester class that I could either extend or pass a block to for use for any testing.

Is it possible to pass a block of code for a method to run? If this is not possible or practical, what is the best way to write a class so it can be extended?

–CODE–

package Lab1;

/**
 * @author $RMDan
 * @date   10-Sep-2012
 * @class  Tester
 * @desc   A Tester class for testing Java classes
 *         For use in Itec 251
 */

import java.io.*; 
import java.util.*;

public class Tester {

    //members
    private String logS;

    //Constructors
    //Default to start == true

    public Tester(){
        this(true);
    }

    public Tester(Boolean start){
        if(start == true){
            this.start();
        }
    }

    public final int start(){
        int exit;
        exit = test();
        //this.displayLog();
        this.exportLog("Test_Employee.Log");
        return exit;
    }


    //Change the contents of this method to perform the test
    private int test(){

        return 0;
    }

    private void log(String message){
        this.log(message,true);
    }

    private void log(String message, boolean display){
        this.logS += message + "\n";

        if(display==true){
            System.out.println(message);
        }
    }

    private void displayLog(){
        System.out.print(this.logS);
    }

    private void exportLog(String file){

        try{
            String output = this.logS;
            output = output.replaceAll("\n", System.getProperty("line.separator"));
            try (BufferedWriter out = new BufferedWriter(new FileWriter(file + ".txt"))) {
                out.write(output);
            }
        }
        catch(IOException e){
            System.out.println("Exception ");
        }

    }
}

Note: the final in the declaration of the start() method is there to shut up the compiler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:40:43+00:00Added an answer on June 11, 2026 at 5:40 pm

    In java, “passing a block” is done by anonymous classes: on-the-fly implementations of an interface or class. You could use an existing interface like Runnable, or create your own interface that returns a value, for example:

    interface MyTest {
        int test();
    }
    

    then alter your code to expect one of these:

    public final int start(MyTest myTest) {
        ...
        exit = myTest.test();
        ...
    }
    

    then to use it anonymously, call your start method with an anonymous MyTest:

    start(new MyTest() {
        public int test() {
            // do something
        } 
    })l
    

    You will definitely get more points for using anonymous classes!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a StringOutputStream class in Java because I need the pre-Base64-encoded data
I'm writing a class in Java to represent a Graph data structure. This is
I'm writing an app for Java ME, and I need a class for holding
Is there a C or C++ compatible library for reading and writing Java class
first of all, a preface: I'm writing a java class that creates temporary tables
I have some experience writing web applications in Java for class projects. My first
I'm writing an abstract class in Java where I'd like a parameter of one
I am writing a Xml Tag Renamer class with Java which reads in a
I have a question about best practices in Java. I'm writing a class, which
Is there any class provided in the Java standard API that allows file structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.