D
D
dontgiveafcuk2016-01-09 00:04:14
Java
dontgiveafcuk, 2016-01-09 00:04:14

How to extend a java class with java.io?

There is a GetSet class that needs to be extended with a built-in class from the java.io* package

package com.firstproject.getset;

public class GetSet  {
  
  
  private int intVariable;
  private double doubleVariable;
  private String stringVariable;
  
  public int getIntVariable(){
    return intVariable;
  }
  
  public void setIntVariable(int newIntVariable){
    intVariable = newIntVariable;
  }
  
  public double getDoubleVariable(){
    return doubleVariable;
  }
  
  public void setDoubleVariable(double newDoubleVariable){
    doubleVariable = newDoubleVariable;
  }
  
  public String getStringVariable(){
    return stringVariable;
  }
  
  public void setStringVariable(String newStringVariable){
    stringVariable = newStringVariable;
  }

  public void message(){
    
    System.out.println("Int variable = "+intVariable+
        "\nDouble variable = "+doubleVariable+
        "\nString variable = "+"\""+stringVariable+"\"");
    
  }
}

The goal is to create an object in the main class based on GetSet that would contain the BufferReader functions,
that is, I need to make sure that I do not assign values ​​to variables in the main class itself, but do this by reading the lines entered into the console.
here is the Main class
import java.io.*;
import com.firstproject.getset.*;

public class Main{
  public static void main(String[] args){
    GetSet getset = new GetSet(Reader);
    getset.setIntVariable(1);
    getset.setDoubleVariable(6.66);
    getset.setStringVariable("Hello, World!");
    
    getset.message();
  }
}

and you need something like this:
import java.io.*;
import com.firstproject.getset.*;

public class Main{
  public static void main(String[] args){
    GetSet getset = new GetSet(InputStreamReader(System.in));
    getset.setIntVariable = getset.readLine();
    getset.setDoubleVariable = getset.readLine();
    getset.setStringVariable = getset.readLine();
    getset.message();
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Dorofeev, 2016-01-09
@TechCloud

So, and do as you yourself said - expand.
In this case, "inherit":

import java.io.BufferedReader;
import java.io.Reader;

public class GetSet extends BufferedReader {
    private int intVariable;
    private double doubleVariable;
    private String stringVariable;

    public GetSet(Reader in, int sz) {
        super(in, sz);
    }

    public GetSet(Reader in) {
        super(in);
    }

    public int getIntVariable() {
        return intVariable;
    }

    public void setIntVariable(int newIntVariable) {
        intVariable = newIntVariable;
    }

    public double getDoubleVariable() {
        return doubleVariable;
    }

    public void setDoubleVariable(double newDoubleVariable) {
        doubleVariable = newDoubleVariable;
    }

    public String getStringVariable() {
        return stringVariable;
    }

    public void setStringVariable(String newStringVariable) {
        stringVariable = newStringVariable;
    }

    public void message() {

        System.out.println("Int variable = " + intVariable +
                "\nDouble variable = " + doubleVariable +
                "\nString variable = " + "\"" + stringVariable + "\"");

    }
}

The only remark,
In Java, such a notation is incorrect:
Function parameters are set like this:
You can change the function a little:
public void setIntVariable(String s) {
        intVariable = Integer.parseInt(s);
    }

C
cthulhudx, 2016-01-09
@cthulhudx

Read the first volume of Horstmann

D
dontgiveafcuk, 2016-01-09
@dontgiveafcuk

I'm a total noob at JAAA, just trying to improvise and improve my learning process with google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question