| |

VerySource

 Forgot password?
 Register
Search
View: 850|Reply: 8

Being a novice is really painful! I don't understand such a simple question. How can we inherit classes?

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-6 16:40:01
| Show all posts |Read mode
Big 3, just after learning java, the following program seems to be very long, please help me heroes ctrl + c look at it. I want to build a separate library class, why can't I build it? Maybe it's my concept.
Thank you!
import java.io. *;
import java.util. *;
public class stock
{
String name;
String author;
int price;
stock (String a, String b, int c)
{
name = a;
author = b;
price = c;
}
void print ()
{
System.out.println ("name is" + name);
System.out.println ("author is" + author);
System.out.println ("price is" + price);
}
void get ()
{
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
}
}
class process
{
void step1 ()
{
System.out.println ("do what?");
System.out.println ("a.search b.add c.exit");
char ch = 0;
try
{
ch = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}
if (ch == 'a')
{
System.out.println ("a.by name b.by author");
Ranch
char ch2 = 0;
try
{
ch2 = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}
if (ch2 == 'a')
{
System.out.print ("plz input the book's name:");
InputDate fuck = new InputDate ();
fuck.findbookname ();
}
Ranch
if (ch2 == 'b')
{
System.out.print ("plz input the author's name:");
InputDate fuck2 = new InputDate ();
fuck2.findauthorname ();
}
}
if (ch == 'b')
{
InputDate fuck3 = new InputDate ();
fuck3.add ();
}
if (ch == 'c')
{
System.out.println ("c u");
}
}
}
class InputDate
{
static private String s;
static public void input ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try {
s = br.readLine ();
} catch (IOException e) {}
Ranch
}
static public String getString ()
{
input ();
return s;
Ranch
}
static public int getInt ()
{
input ();
return Integer.parseInt (s);
}
public void findbookname ()
{
    Ransom
/ * int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15); * /
    stock mis = new stock ();
    mis.get ();
    s = InputDate.getString ();
    Vector names = new Vector ();
    for (int i = 0; i <a.length&&a [i + 1]! = null; i ++)
    {
    names.add (a [i] .name);
    }
    if (names.indexOf (s)! =-1)
    {
    System.out.println ("u can fint it here!");
    }
    else
    System.out.println ("sorry, there is not such a book!");
    Ransom
}
    public void findauthorname ()
{
    s = InputDate.getString ();
    Ransom
    / * int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15); * /
    Vector authornames = new Vector ();
    for (int i = 0; i <a.length&&a [i + 1]! = null; i ++)
    {authornames.add (a [i] .author);}
    if (authornames.indexOf (s)! =-1)
    {
    Ransom
    System.out.println ("there is such a book!");}
    else
    System.out.println ("i didn't hear that pl at all!");
    Ransom
    }
    public void add ()
    {
    / * int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15); * /
System.out.println ("plz input info:");
String ab, b;
int c;
ab = InputDate.getString ();
b = InputDate.getString ();
c = InputDate.getInt ();
int i = 0;
while (a [i]! = null) i ++;
a [i] = new stock (ab, b, c);
System.out.println ("successfully added");
process a1 = new process ();
a1.step1 ();
    }
}
class main
{
public static void main (String args [])
{
process a1 = new process ();
a1.step1 ();
}
}
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-6 20:39:02
| Show all posts
Can't the comment be run without the comment. Copy it and see:

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

class stock
{
String name;
String author;
int price;

stock ()
{
System.out.println ("Constructor without parameters");
}

stock (String a, String b, int c)
{
name = a;
author = b;
price = c;
}

void print ()
{
System.out.println ("name is" + name);
System.out.println ("author is" + author);
System.out.println ("price is" + price);
}

void get ()
{
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
}
}


class process
{
void step1 ()
{
System.out.println ("do what?");
System.out.println ("a.search b.add c.exit");
char ch = 0;

try
{
ch = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}

if (ch == 'a')
{
System.out.println ("a.by name b.by author");
char ch2 = 0;

try
{
ch2 = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}

if (ch2 == 'a')
{
System.out.print ("plz input the book's name:");
InputDate fuck = new InputDate ();
fuck.findbookname ();
}

if (ch2 == 'b')
{
System.out.print ("plz input the author's name:");
InputDate fuck2 = new InputDate ();
fuck2.findauthorname ();
}
}

if (ch == 'b')
{
InputDate fuck3 = new InputDate ();
fuck3.add ();
}

if (ch == 'c')
{
System.out.println ("c u");
}
}
}

class InputDate
{
static private String s;

static public void input ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try {
s = br.readLine ();
} catch (IOException e) {}
}

static public String getString ()
{
input ();
return s;
}
Ransom
static public int getInt ()
{
input ();
return Integer.parseInt (s);
}

public void findbookname ()
{
Ranch
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
// stock mis = new stock ();
//mis.get ();
s = InputDate.getString ();
Vector names = new Vector ();

for (int i = 0; i <a.length&&(a [i + 1]! = null); i ++)
{
names.add (a [i] .name);
}

if (names.indexOf (s)! =-1)
{
System.out.println ("u can fint it here!");
}
else
System.out.println ("sorry, there is not such a book!");
Ranch
}

    public void findauthorname ()
{
s = InputDate.getString ();
    
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
    
Vector authornames = new Vector ();
for (int i = 0; i <a.length&&a [i + 1]! = null; i ++)
{authornames.add (a [i] .author);}
Ranch
if (authornames.indexOf (s)! =-1)
{
System.out.println ("there is such a book!");}
else
System.out.println ("i didn't hear that pl at all!");
}

    public void add ()
    {
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
System.out.println ("plz input info:");
String ab, b;
int c;
ab = InputDate.getString ();
b = InputDate.getString ();
c = InputDate.getInt ();
int i = 0;

while (a [i]! = null) i ++;
a [i] = new stock (ab, b, c);
System.out.println ("successfully added");
process a1 = new process ();
a1.step1 ();
}
}

public class Tain
{
public static void main (String args [])
{
process a1 = new process ();
a1.step1 ();
}
}
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-7 12:39:01
| Show all posts
LZ wrong places and reasons to help you mark, it is recommended to read more
package huangwei;
import java.io. *;
import java.util. *;
 class stock
{
String name;
String author;
int price;
stock (String a, String b, int c)
{
name = a;
author = b;
price = c;
}
void print ()
{
System.out.println ("name is" + name);
System.out.println ("author is" + author);
System.out.println ("price is" + price);
}
void get ()
{
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
}
}
class process
{
void step1 ()
{
System.out.println ("do what?");
System.out.println ("a.search b.add c.exit");
char ch = 0;
try
{
ch = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}
if (ch == 'a')
{
System.out.println ("a.by name b.by author");

char ch2 = 0;
try
{
ch2 = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {}
if (ch2 == 'a')
{
    System.out.print ("plz input the book's name:");
    InputDate fuck = new InputDate ();
    fuck.findbookname ();
}

if (ch2 == 'b')
{
System.out.print ("plz input the author's name:");
InputDate fuck2 = new InputDate ();
fuck2.findauthorname ();
}
}
if (ch == 'b')
{
InputDate fuck3 = new InputDate ();
fuck3.add ();
}
if (ch == 'c')
{
System.out.println ("c u");
}
}
}
class InputDate
{
static private String s;
static public void input ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try {
s = br.readLine ();
} catch (IOException e) {}

}
static public String getString ()
{
input ();
return s;

}
static public int getInt ()
{
input ();
return Integer.parseInt (s);
}
public void findbookname ()
{

int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
   stock mis = new stock ("Think in JAVA", "LXX", 100);
   // The parameters of the constructor in stock are 3, but you don't add parameters directly
   // Each class has a default parameterless constructor. When you create a new constructor, the original default parameterless method is gone
    // stock mis = new stock ();
    mis.get ();
    s = InputDate.getString ();
    Vector names = new Vector ();
    for (int i = 0; i <a.length&&a [i + 1]! = null; i ++)
    {
    names.add (a [i] .name);
    }
    if (names.indexOf (s)! =-1)
    {
    System.out.println ("u can fint it here!");
    }
    else
    System.out.println ("sorry, there is not such a book!");

}
    public void findauthorname ()
{
    s = InputDate.getString ();

   int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
    Vector authornames = new Vector ();
    for (int i = 0; i <a.length&&a [i + 1]! = null; i ++)
    {authornames.add (a [i] .author);}
    if (authornames.indexOf (s)! =-1)
    {

    System.out.println ("there is such a book!");}
    else
    System.out.println ("i didn't hear that pl at all!");

    }
    public void add ()
    {
    int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
System.out.println ("plz input info:");
String ab, b;
int c;
ab = InputDate.getString ();
b = InputDate.getString ();
c = InputDate.getInt ();
int i = 0;
while (a [i]! = null) i ++;
a [i] = new stock (ab, b, c);
System.out.println ("successfully added");
process a1 = new process ();
a1.step1 ();
    }
}
class main
{
public static void main (String args [])
{
process a1 = new process ();
a1.step1 ();
}
}
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-8 11:27:01
| Show all posts
import java.io. *;
import java.util. *;

/ **
 * Changed places
 * You are using the public modifier,
 * If you write so many classes under the same .java original file,
 * Then your public should be added to your entry class (here refers to the main class you wrote)
 * /
class stock {
String name;

String author;

int price;
Ranch
/ **
* Changed place, your stock class only has a constructor with parameters
* But you use empty parameter construction when you instantiate, so you have to write a constructor without parameters
* /
public stock () {}
Ranch
stock (String a, String b, int c) {
name = a;
author = b;
price = c;
}

void print () {
System.out.println ("name is" + name);
System.out.println ("author is" + author);
System.out.println ("price is" + price);
}
/ *
* Changed places you want in the findbookname method
* An array of the stock class is used. If you use it directly, the compiler cannot recognize
*
* Change the scheme, write this class as a method that returns an array of stock class.
* * /
stock [] get () {
int N = 100;
stock [] a = new stock [N];
a [0] = new stock ("Java", "PlayGrrrr ...", 12);
a [1] = new stock ("C #", "Bill Gates", 13);
a [2] = new stock ("Delphi", "Boxer", 14);
a [3] = new stock ("Oracle", "zy", 15);
Ranch
return a;
}
}

class process {
void step1 () {
System.out.println ("do what?");
System.out.println ("a.search b.add c.exit");
char ch = 0;
try {
ch = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {
}
if (ch == 'a') {
System.out.println ("a.by name b.by author");

char ch2 = 0;
try {
ch2 = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {
}
if (ch2 == 'a') {
System.out.print ("plz input the book's name:");
InputDate fuck = new InputDate ();
fuck.findbookname ();
}

if (ch2 == 'b') {
System.out.print ("plz input the author's name:");
InputDate fuck2 = new InputDate ();
fuck2.findauthorname ();
}
}
if (ch == 'b') {
InputDate fuck3 = new InputDate ();
fuck3.add ();
}
if (ch == 'c') {
System.out.println ("c u");
}
}
}

class InputDate {
static private String s;

static public void input () {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try {
s = br.readLine ();
} catch (IOException e) {
}

}

static public String getString () {
input ();
return s;

}

static public int getInt () {
input ();
return Integer.parseInt (s);
}

public void findbookname () {

stock mis = new stock ();
stock a [] = mis.get (); // Here we use the modified get method
s = InputDate.getString ();
Vector names = new Vector ();
for (int i = 0; i <a.length&&a [i + 1]! = null; i ++) {
names.add (a [i] .name);
}
if (names.indexOf (s)! = -1) {
System.out.println ("u can fint it here!");
} else
System.out.println ("sorry, there is not such a book!");

}

public void findauthorname () {
s = InputDate.getString ();
/ * You use a, but it is not provided. If you use it multiple times,
* It is recommended that you use the stock class array (here, a, and the findbookname method a above and the a in the add method below)
* As a property of your class, initialize in the constructor,
* Or assign a value when declaring a property * /
stock mis = new stock ();
stock a [] = mis.get ();

Ranch
Vector authornames = new Vector ();
for (int i = 0; i <a.length&&a [i + 1]! = null; i ++) {
authornames.add (a [i] .author);
}
if (authornames.indexOf (s)! = -1) {

System.out.println ("there is such a book!");
} else
System.out.println ("i didn't hear that pl at all!");

}

public void add () {
Ranch
System.out.println ("plz input info:");
String ab, b;
int c;
ab = InputDate.getString ();
b = InputDate.getString ();
c = InputDate.getInt ();
int i = 0;
Ranch
/ *
* Changed place for the same reasons
* * /
stock mis = new stock ();
stock a [] = mis.get ();
Ranch
while (a [i]! = null)
i ++;
a [i] = new stock (ab, b, c);
System.out.println ("successfully added");
process a1 = new process ();
a1.step1 ();
}
}

/ *
 * Modified places with public modifier. See above for reasons
 * * /
public class main {
public static void main (String args []) {
process a1 = new process ();
a1.step1 ();
}
}
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-9 14:27:01
| Show all posts
Thank you heroes, it seems thatshenjiaenfigured out what I wanted to ask, and the answer was the best. Sicily. thank you all.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-9 16:09:01
| Show all posts
---------- javac ----------
Note: main.java uses unchecked or unsafe operations.
Note: To learn more, use -Xlint: unchecked to recompile.

Output completed (1 second)-Normal termination
Why is this happening when compiling?
There is a problem there! ~
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-9 16:36:01
| Show all posts
According to theshenjiaenreform, my newly added book cannot be found in the query. It is because my add book function has not been implemented or what is going on. It is currently being debugged.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-25 22:45:01
| Show all posts
I'm sweating. I haven't been on verysource for a few days, so people have a talk. Your newly added book cannot be found, it is your logic problem. You re-instantiate and operate in many places, and the objects you refer to are not the same ....... There are many places like this!
  I changed it according to your needs. I will show you the code

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

class stock {
The
private String name;

private String author;

private int price;
The
// all books
private ArrayList books = new ArrayList ();
The
public stock () {
// Add book during initialization
this.books.add (new stock ("Java", "PlayGrrrr ...", 12));
this.books.add (new stock ("C #", "Bill Gates", 13));
this.books.add (new stock ("Delphi", "Boxer", 14));
this.books.add (new stock ("Oracle", "zy", 15));
}

public stock (String a, String b, int c) {
name = a;
author = b;
price = c;
}
The
/ * Expose getters and setters for all properties * /
public void print () {
System.out.println ("name is" + name);
System.out.println ("author is" + author);
System.out.println ("price is" + price);
}

public String getAuthor () {
return author;
}

public void setAuthor (String author) {
this.author = author;
}

public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

public int getPrice () {
return price;
}

public void setPrice (int price) {
this.price = price;
}

public ArrayList getBooks () {
return books;
}

public void setBooks (ArrayList books) {
this.books = books;
}
}

class process {
private InputDate fuck = new InputDate (); // operate the same object
void step1 () {
BufferedReader read = new BufferedReader (new InputStreamReader (System.in));
String res = "";
char c [] = {};
try {
do
{
System.out.println ("do what?");
System.out.println ("1.search 2.add 3.exit");
int flag = Integer.parseInt (read.readLine ());
switch (flag)
{
case 1:
System.out.println ("a.by name b.by author");
char ch2 = 0;
try {
ch2 = (char) System.in.read ();
System.in.skip (2);
} catch (IOException z) {
}
if (ch2 == 'a') {
System.out.print ("plz input the book's name:");
fuck.findbookname ();
}

if (ch2 == 'b') {
System.out.print ("plz input the author's name:");
fuck.findauthorname ();
}
break;
case 2:
fuck.add ();
break;
case 3:
System.out.println ("c u");
break;
default:
System.out.println ("your input has false!");
break;
}
System.out.println ("again? Y / n");
res = read.readLine ();
c = res.toCharArray ();
}
while (c [0] == 'Y' || c [0] == 'y');
System.out.println ("Welcome Next!");
} catch (NumberFormatException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}

class InputDate {
The
private stock mis = new stock (); // operate the same object
The
static private String s;
static public void input () {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
try {
s = br.readLine ();
} catch (IOException e) {
}
}

static public String getString () {
input ();
return s;

}

static public int getInt () {
input ();
return Integer.parseInt (s);
}
/ **
* Find books by name
* Retrieve all books in stock category
* /
public void findbookname () {
The
s = InputDate.getString ();
The
ArrayList books = mis.getBooks ();
The
for (Iterator iter = books.iterator (); iter.hasNext ();)
{
if (((stock) iter.next ()). getName (). equals (s))
{
System.out.println ("u can fint it here!");
return;
}
}
System.out.println ("sorry, there is not such a book!");
}
  // Find books by author
public void findauthorname () {
The
s = InputDate.getString ();
The
ArrayList books = mis.getBooks ();
The
for (Iterator iter = books.iterator (); iter.hasNext ();)
{
if (((stock) iter.next ()). getAuthor (). equals (s))
{
System.out.println ("u can fint it here!");
return;
}
}
System.out.println ("sorry, there is not such a book!");

}
// add books
public void add () {

System.out.println ("plz input info:");
String ab, b;
int c;
ab = InputDate.getString ();
b = InputDate.getString ();
c = InputDate.getInt ();
The
ArrayList books = mis.getBooks ();
books.add (new stock (ab, b, c));
System.out.println ("successfully added");
}
}

public class main {
public static void main (String args []) {
process a1 = new process ();
a1.step1 ();
}
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-6 19:00:01
| Show all posts
very long...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list