Java - Using an object inside an object - java

I am working on a project ( I had a problem yesterday and so many people helped me!) so I decided to ask for help again.
My code has 3 classes. ProjectMain,Students,Classroom. I created an array of Classroom objects. Right now I have 3 Classroom objects. But I have to assign student objects to these Classroom objects. For example : classarray[0] is an object from Classroom class and studentobject.get(0) , studentobject.get(1) ... will be students objects inside classarray[0] object. But I have failed on this while coding. Here are my classes :
public class Classroom
{
private String classname;
private String word[] = null;
protected ArrayList<Students> studentobject = new ArrayList<Students>(10);
public String[] getWord()
{
return word;
}
public void setWord(String[] word)
{
this.word = word;
}
public ArrayList<Students> getStudentobject()
{
return studentobject;
}
public void setStudentobject(ArrayList<Students> studentobject)
{
this.studentobject = studentobject;
}
public String getClassname()
{
return classname;
}
public void setClassname(String classname)
{
this.classname = classname;
}
public void classroomreader(String filename)
{
// This method gets the name of Classroom
File text = new File("C:/Users/Lab/Desktop/classlists/" + filename
+ ".txt");
Scanner scan;
try
{
scan = new Scanner(text);
String line = scan.nextLine();
word = line.split("\t");
line = scan.nextLine();
word = line.split("\t");
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
This is my student class :
public class Students extends Classroom
{
private String name,id;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
And my main class :
public class ProjectMain
{
public static void main(String[] args)
{
Classroom[] classarray = new Classroom[3];
//I got 3 Classroom objects here
classarray[0]=new Classroom();
classarray[1]=new Classroom();
classarray[2]=new Classroom();
classarray[0].classroomreader("class1");
classarray[0].studentobject.get(0).setName(classarray[0].getWord()[1]);
//The problem is in here. When my code comes to the line above,
// at java.util.ArrayList.rangeCheck(Unknown Source) error comes out.
// I tried to get first object in studentobject Arraylist, and tried to set it's name
// to the variable which my text reader reads.
How can I write what I have in my mind?

Your classroomreader method reads the file but don't do much of it... maybe you want to create some instance of Students within it.
scan = new Scanner(text);
String line = scan.nextLine();
word = line.split("\t"); // won't be used
line = scan.nextLine();
word = line.split("\t"); // erased here
There you only have the last line (split) of the file in word attribute.

When creating Classroom instance studentobject list is created empty and it stays that way so you can't access first (or any) object in it.
To populate your list you may add to Classroom method like this:
public void addStudent(Student s)
{
studentobject.add(s);
}

classroom contains the following field declaration
String word[] = null;
the main class, incl the classroomreader does not set a value to this field. Yet you are going to invoke
classarray[0].getWord()[1]
which then must fail.
tip: don't use expressions like this, which can be found in your main class (at least not in early stages of development, or learning)
classarray[0].studentobject.get(0).setName(classarray[0].getWord()[1]);
resolve into variables and several steps. Compilers are smart enough to produce the same code if the context is not disturbed, ie the long expression is resolved into a single block.
Never forget that the purpose of programming languages is to make programs readable for humans. :) Code with abbreviations or "tricks" simply shows some philodoxical attitude (imho)

Related

Java printing Class objects in ArrayList

Sailor class
public class Sailor {
private String name;
private String email;
public Sailor(String name, String email) {
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Crew class
import java.util.ArrayList;
public class Crew {
private ArrayList<Sailor> sailorList = new ArrayList<>();
public Crew() {
}
public void addCrewMember(Sailor sailor) {
sailorList.add(sailor);
}
public String toString() {
String str = "";
for(int i = 0; i < sailorList.size(); i++) {
str = sailorList.get(i).getName() + sailorList.get(i).getEmail();
}
return str;
}
}
Main program
public class ObjectsSailorProgram {
public static void main(String[] args) {
Sailor firstSailor = new Sailor("Frank", "frank#mail.com");
Sailor secondSailor = new Sailor("Susan", "susan#mail.com");
Sailor thirdSailor = new Sailor("John", "john#sailors.com");
Sailor fourthSailor = new Sailor("Ann", "ann#sailors.com");
Crew firstCrew = new Crew();
Crew secondCrew = new Crew();
firstCrew.addCrewMember(firstSailor);
firstCrew.addCrewMember(secondSailor);
firstCrew.addCrewMember(fourthSailor);
secondCrew.addCrewMember(thirdSailor);
secondCrew.addCrewMember(secondSailor);
System.out.println("=== First crew ===\n" + firstCrew);
System.out.println("=== Second crew ===\n" + secondCrew);
secondSailor.setEmail("Susan#sailors.com");
System.out.println("=== Second crew ===\n" + secondCrew);
}
}
I am having trouble printing the crews and I'm not sure if the addCrewMember is correct.
I've tried reading other similar posts but i haven't been able to use the solutions here. So i need help with the addCrewMember and toString methods
You aren't setting the variables neither in constructor nor by calling setters in your main.
You can change the constructor as below:
public Sailor(String name, String email) {
this.name = name;
this.email = email;
}
In your toString() method, you're overwriting the str. Use StringBuilder to append strings into the resulting string. You can modify your toString() method as follows:
public String toString() {
StringBuilder str = new StringBuilder();
for(int i = 0; i < sailorList.size(); i++) {
str.append(i+1)
.append(". ")
.append(sailorList.get(i).getName())
.append(" ")
.append(sailorList.get(i).getEmail())
.append("\n");
}
return str.toString();
}
And your program will give the output:
=== First crew ===
1. Frank frank#mail.com
2. Susan susan#mail.com
3. Ann ann#sailors.com
=== Second crew ===
1. John john#sailors.com
2. Susan susan#mail.com
=== Second crew ===
1. John john#sailors.com
2. Susan Susan#sailors.com
0> sailor's constructor is wrong-> you're not initializing the data
public Sailor(String name, String email) {
setEmail(email); //or this.email=email;
this.name=name; //you may make a setter also for this
}
1> private ArrayList<Sailor> sailorList = new ArrayList<Sailor>();
2> the to-string is also wrong: it prints just the last sailor, use += instead of =
str +=" "+ sailorList.get(i).getName() + sailorList.get(i).getEmail();
This is just personal taste: I prefer "for-each" syntax.
for(Sailor s: sailorList){
out += s.getName()+" "+s.getEmail();
}
As others have noted, you are not assigning the values in your Sailor constructor so do that. You should also add any missing get/set methods.
public Sailor(String name, String email) {
this.name = name;
this.email = email;
}
The addCrewMember(Sailor) method in Crew looks correct but your toString() method is flawed.
You are trying to build a String but you are re-assigning it on each iteration rather then concatenating elements on each iteration.
Except in very simple cases, you should avoid String concatenation when compose Strings.
The code is pushing all of the elements together with no separation which means you will have an unreadable blob of characters (e.g. bobsmithbsmith#boat.comsamsmithssmith#boat.com)
I would suggest that you write a toString() in Sailor class. This example separates the elements of Sailor by commas and wraps the entire entity in curly braces (e.g. "{Sam Smith, ssmith#boat.com}"). Note the use of String.format(..) rather than concatenation.
public class Sailor {
... stuff ...
#Override public String toString() {
return String.format("{%s, %s}", name, email);
}
}
The Crew.toString() method can employ this to compose its String. The following makes use of the StringJoiner class to compose a comma-separated list of elements wrapped in square braces to represent the crew members (e.g. "[{Al Almond, aalmond#boat.com},{Bob Bobson, bbobson#boat.com}]");
public class Crew {
... stuff ...
#Override public String toString() {
StringJoiner sj = new StringJoiner(",", "[", "]");
for (Sailor s : sailorList) {
sj.add(s.toString()); // Sailor.toString() is explicitly invoked here but you could remove that call and it should still be invoked implicitly
}
return sj.toString();
}
}

How to use setter to set a value to a attribute which is a private array?

I had made an array of roll number so how to give a user input using setter to the private attribute which is roll number.
I made an object of class Students which is a students and tried thisstudents.for(int i=0;i<n;i++)
{(setRollno[i](sc.next()))};
But it did not worked.
class Students{
private String[] rollno = new String[1000];
private int[] intel = new int[1000];
private int[] type = new int[1000];
private String[] name = new String[1000];
public void setRollno(String[] rollno) {
this.rollno = rollno;
}
public void setName(String[] name) {
this.name = name;
}
public void setIntel(int[] intel) {
this.intel = intel;
}
public void setType(int[] type) {
this.type = type;
}
public String[] getRollno() {
return rollno;
}
public String[] getName() {
return name;
}
public int[] getIntel() {
return intel;
}
public int[] getType() {
return type;
}
}
Possibly I'm misunderstanding the aim of your data structure, but it sounds like you're trying to create a collection of student data all in one place. If this is the case then I'd strongly recommend creating a Student class which represents just one student at a time, and then using one or more standard types from the Java Collection Framework to make it easy to find a particular student.
For example, your Student class could simply contain fields which hold the roll number, name, and other data for one student. Then, assuming that you'll want to find students quickly by roll number you could create a Map<String, Student> where the map keys are roll numbers and the corresponding map value is the student who has that roll number. Simply put a new Student object into the Map after constructing it. If you need to find students by name then you could create a Map<String, Collection<Student>> (because more than one student might have the same name) and put each new Student object into this name map after constructing it.
This is likely to lead to code which is a lot easier to read, maintain, and use than an all-in-one custom collection class such as the one shown in your question.
As a rough code example:
String rollNumber = getNewRollNumber(); // wherever roll numbers come from
String name = getStudentName(); // wherever the name comes from
Student newStudent = new Student(rollNumber, name, etc);
studentsByRollNumber.put(rollNumber, newStudent);
studentsByName.computeIfAbsent(name,
n -> new ArrayList<>(1)).add(student);
Student studentWithParticularRollNumber =
studentsByRollNumber.get("123456");
Collection<Student> studentsWithParticularName =
studentsByName.get("Perry, Fred");
The Map#computeIfAbsent method will create a new ArrayList under the given student name only if no entry already exists under that name, or will fetch the existing list if that name already exists in the map, and then will put the new student into the list.
Within the call to computeIfAbsent the lambda expression t -> new ArrayList<>(1) simply means "take the value of the map key, and whatever it is just create a new ArrayList of size one". It simply guarantees that if there is not already a Collection<Student> stored under the given student name then a new ArrayList<Student> will be created and stored there.
setRollno[i](sc.next()); isn't the good syntax. Your function setRollno take an array of strings as parameter, and change all the array you have. If that's what you want, you must pass an array of Strings as parameter:
If you want to set one specific String in your rollno, you must create another function:
setRollNoAtIndex(int i, String s) {
this.rollno[i] = s;
}
If you need to call this in a loop, you can then simply do:
for(int i=0; i < n ;i++) {
students.setRollNoAtIndex(i, sc.next());
}
As per Berger's comment:
The syntax you were trying to use was probably either:
for(int i=0; i < n ;i++) {
students.getRollno()[i] = sc.next();
}
or
String[] list = new String [1000];
for(int i=0; i < n ;i++) {
list[i] = sc.next();
}
students.setRollno(list);
I think that design of this class is not correct, because we have access to internal arrays directly. Moreover, in that case you do not have to init all these arrays on new instance creation. I offer a little bit another implementation of this class, that I found more suitable for this goal:
final class Students {
private static final int TOTAL = 1000;
private final Map<String, Student> students = new HashMap<>(TOTAL);
public void setName(String rollno, String name) {
getOrCreateStudent(rollno).name = name;
}
public void setIntel(String rollno, int intel) {
getOrCreateStudent(rollno).intel = intel;
}
public void setType(String rollno, int type) {
getOrCreateStudent(rollno).type = type;
}
public Set<String> getRollno() {
return students.keySet();
}
public String getName(String rollno) {
return students.getOrDefault(rollno, Student.NULL).name;
}
public int getIntel(String rollno) {
return students.getOrDefault(rollno, Student.NULL).intel;
}
public int getType(String rollno) {
return students.getOrDefault(rollno, Student.NULL).type;
}
private Student getOrCreateStudent(String rollno) {
Student student = students.get(rollno);
if (student == null)
students.put(rollno, student = new Student(rollno));
return student;
}
private static final class Student {
private static final Student NULL = new Student(null);
private final String rollno;
private int intel;
private int type;
private String name;
public Student(String rollno) {
this.rollno = rollno;
}
}
}

New to OOP, manage variables in classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So. I got a mission from my teacher to make a program that manages different students. The program will hold the name, education, information and points.
You will have the option to:
Add new students
Change education and information
Manage points
Making new students is not a problem, but managing education, info and points for specific students is the hard part, that's where I need your help. My main.java does not contain anything for now.
Student.java
package student;
public class Student {
String namn;
String program;
String info;
int points;
public void managePoints(){
}
public void changeProgram(){
}
public void changeInfo(){
}
}
Main.java
package student;
public class main {
public static void main(String[] args) {
}
}
According to the comments, I guess the three methods in your class are supposed to change the points, program and info of the student to a desired value. In Java, we call these setters.
You should rename your methods to setPoints, setProgram and setInfo. It's a pattern, you know.
Next, how are you going to know the "desired" value of those fields? You might say, I get them from the text boxes in the methods. But a better approach would be to get the values from another method and pass the value to the setters as a parameter.
To add a parameter to your methods, add a variable-like thingy in the brackets of the method declaration:
public void setPoints (int p)
And for the setInfo
public void setInfo (String i)
And so on.
In the method bodies, you set the fields to the parameters. E.g. In the setInfo method you write
info = i;
I think you can figure out the others.
Now how do you use these methods? For instance, suppose you have a student variable called student. And you got the info of him/her and stored it in a string variable called studentInfo. You can set the student variable's info to studentInfo by
student.setInfo (studentInfo);
Or if you don't want to use a variable, you can just use a string literal.
student.setInfo("this is my info. Blah blah blah");
I don't exactly know what do you want to actually do but your Student class (if I think correctly what you will need) should look more like this:
public class Student {
private String name; // private because you don't want anyone to interact with the variable too much.
private String program;
private String info;
private int points;
public Student( String name, String program, String info, int points ) { // contructor with variables to initialize. You can remove some of the variables if you do not consider they should be here.
this.name = name;
this.program = program;
this.info = info;
this.points = points;
// without `this` you would change parameter's value to itself which isn't what you want.
}
public String getName( ) { // getter because I guess you would like to know students name
return name;
}
public int getPoints( ) {
return points;
}
public void addPoints( int points ) { // setter so you can modify points
this.points += points;
}
public String getProgram( ) { // same as getName
return program;
}
public void setProgram( String program ) {
this.program = program;
}
public String getInfo( ) {
return info;
}
public void setInfo( String info ) {
this.info = info;
}
}
But how to use these methods? You use them as the example below shows
Student s1 = new Student("Abc Xyz", "IT", "Some informations", 12);
Student s2 = new Student("Cba Zyx", "Some other program", "Some more informations, 0);
s2.setInfo( s1.getInfo( ) );
s1.setPoints(1234);
s2.setProgram("Axbzcy");
Getter is a method which returns (most likely) private variable's value.
Setter is a method which sets private variable's value to another value which is passed as a parameter to the method.
Final code:
package student;
// The student class definition
public class Student {
private String name;
private String address;
private String info;
private String kurs;
private int points;
// Constructor
public Student(String name, String address, String info, String kurs, int points) {
this.name = name;
this.address = address;
this.points = points;
this.kurs = kurs;
this.info = info;
}
// Public getter for private variable name
public String getName() {
return name;
}
// Public getter for private variable address
public String getAddress() {
return address;
}
public String getInfo() {
return info;
}
public int getPoints() {
return points;
}
// Public setter for private variable address
public void setAddress(String address) {
this.address = address;
}
public void setPoints(int points){
this.points = points;
}
public void setInfo(String info){
this.info = info;
}
public void setKurs(String kurs){
this.kurs = kurs;
}
// Describe itself
public String toString() {
return name + ", Adress: " + address + ", Info: " + info + ", Kurs: " + kurs + ", Poäng: " + points +" ";
}
}
Main
package student;
// A test driver program for the Student class
public class main {
public static void main(String[] args) {
Student ArHa = new Student("A H", "Jysgaan 61", "ADHD", "Teknik", 5);
ArHa.setPoints(10);
ArHa.setKurs("TEINF");
System.out.println(ArHa);
Student DaSk = new Student("Dael Sklbr", "Fegea 65", "Svart", "Teknik", 5);
DaSk.setInfo("Riktigt svart");
System.out.println(DaSk);
Student FaMe = new Student("Falafel Medusa", "Fågel 123", "Maten", "Kock", 123);
System.out.println(FaMe);
}
}
Thank you everyone for the help.

Java Swing Object[][] getData() confusion

Hi I'm having some trouble getting started with a problem in a Java course learning Swing and starting on JTables and getting data into them. It's going to be hard to explain so I'm just going to post the code I was given, along with the question.
The question is:
The getData() method needs to return an Object[][] containing the data represented by the class.
The first class is MusicAlbum
class MusicAlbum {
private String id;
private String name;
private String genre;
private boolean isCompilation;
private int track_count;
public MusicAlbum(String id, String name, String genre, boolean isCompilation, int track_count) {
this.id = id;
this.name = name;
this.genre = genre;
this.isCompilation = isCompilation;
this.track_count = track_count;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGenre() {
return genre;
}
public boolean isCompilation() {
return isCompilation;
}
public int getTrackCount() {
return track_count;
}
public boolean equals(Object obj) {
if (obj instanceof MusicAlbum)
return this.id.equalsIgnoreCase(((MusicAlbum)obj).id);
return super.equals(obj);
}
}
The class I have to implement the methods in is MusicDataObject (at the bottom)
import java.util.Random;
import java.util.Scanner;
public class MusicDataObject {
private List<MusicAlbum> albums = new ArrayList<>();
private Random random = new Random(); // for generating IDs
public void addAlbum(MusicAlbum album) throws IllegalArgumentException {
if (searchAlbum(album.getId()) != null)
throw new IllegalArgumentException("Album ID is not new!");
albums.add(album);
}
public MusicAlbum searchAlbum(String id) {
for (MusicAlbum album : albums) {
if (album.getId().equalsIgnoreCase(id)) {
return album;
}
}
return null;
}
public MusicAlbum removeAlbum(String id) {
MusicAlbum album = searchAlbum(id);
albums.remove(album);
return album;
}
public void updateAlbum(MusicAlbum album)
throws IllegalArgumentException {
if (removeAlbum(album.getId()) == null)
throw new IllegalArgumentException("Album ID does not exist!");
addAlbum(album);
}
public String generateID() {
String formatter = "A%0" + (int)Math.ceil(Math.log10(albums.size() * 2) + 1) + "d";
String ID;
do {
ID = String.format(formatter, random.nextInt(albums.size() * 2 + 1));
} while (searchAlbum(ID) != null);
return ID;
}
public void saveData(String fileName) throws IOException {
// make sure that the file exists or try to create it
File fout = new File(fileName);
if (!fout.exists() && !fout.createNewFile())
return;
PrintWriter out = new PrintWriter(fout);
for (MusicAlbum album: albums) {
out.println(serializeAlbum(album));
}
out.close();
}
public String serializeAlbum(MusicAlbum album) {
return String.format(
"%s;%s;%s;%b;%d",
album.getId(),
album.getName(),
album.getGenre(),
album.isCompilation(),
album.getTrackCount());
}
public void loadFile(String fileName) throws FileNotFoundException {
albums = new ArrayList<>();
Scanner in = new Scanner(new File(fileName));
while (in.hasNext()) {
// --- split the next line with the character ";"
String line = in.nextLine();
String[] tokens = line.split(";");
// --- construct a new MusicAlbum using the resulting tokens. NOTE: This isn't very robust.
// If a line doesn't contain enough data or the data is invalid, this will crash
albums.add(new MusicAlbum(
tokens[0],
tokens[1],
tokens[2],
Boolean.parseBoolean(tokens[3]),
Integer.parseInt(tokens[4])
));
}
}
// ----- these methods need to be implemented
public Object[][] getData() {
// TODO
}
public String[] getColumnNames() {
// TODO
}
}
The sample data being used is in a txt file, formatted as so:
A01;Defiance;Soundtrack;true;24
A02;Insomniac;Punk Rock;false;14
A03;A Great Day For The Race;Gypsy Jazz;false;10
A04;Viva La Internet;Ska;false;31
A05;New Surrender;Rock;false;17
So basically it's this getData() method they want me to implement that is giving me grief. I don't fully understand what they want me to do, nor do I fully understand what the Object[][] does.
I hope I have been clear enough, and I will appreciate all help given. Also please try to explain things as best you can and dumb them down as much as possible, I'm new to a lot of this :)
Thanks for your time.
Object[][] is a 2-dimensional array. Each of its element is an Object[], a one-dimensional array.
Your task is to create a 2 dimensional array, having one element (Object[]) for each of your MusicAlbum. An Object[] should hold the properties of a MusicAlbum like id, name, genre, isCompilation and track_count.
You can create an object array like this:
Object[] arr = new Object[] { "some", "values", 23, true };
You can create a 2 dimensional array like this:
Object[][] arr2d = new Object[size][];
And you can iterate over all your MusicAlbums, create an Object[] for each of them containing the properties of that music album, and set it in the arr2d.
You can set/get elements of a 2-dimensional array just like any other arrays:
// Set first element:
arr2d[0] = arr;
// Get first element:
Object[] firstElement = arr2d[0];
The getColumnNames() method should just return a String[] (a String array) containing the column names, the names of the properties.
And it might be obvious but note that the order you return the column names and the order of the property values (in the elements of the Object[]) should be the same.

How to print a String defined in one class in a different class?

I have two classes. In the first one, I used the Scanner to retrieve the user's name and then store it in a String called name. Then say, I start a new class, and want to print that came out, how do I go about it. So I just wrote up this code as an example, so you can get an idea of what I'm trying to ask. I'll post both classes.
import java.util.Scanner;
public class One {
public static void main(String[] args) {
String name;
String start;
Scanner input = new Scanner(System.in);
System.out.println("Hello, what is your name?");
name = input.nextLine();
System.out.println("Hello "+name+", welcome! To ocntinue, please hit any key.");
start = input.nextLine();
if(start != null){
Two object = new Two();
}
}
}
Second class.
public class Two {
public Two() {
System.out.println("Ok "+One.name+", lets start!");
}
}
So, you will probably be doing something like this: -
class One
{
private String name = "bob";
public String getName()
{
return name;
}
public static void main(String [] args)
{
One one = new One();
Two two = new Two(one);
// You could also just pass an r-value to Two, as in, Two(new One()) if you
// never require 'one' again
}
}
class Two
{
public Two(One one)
{
System.out.println("Ok " + one.getName() + ", lets start!");
}
}
What is going on?
Creating two classes in your main entry point method.
Passing the instance of One to the constructor of Two
Two then calls getName()
You could, as others have suggested, pass a string as the constructor; alternatively, you could do both if required as Java supports overloading methods see
Recommendations
Take a look at http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html for overriding methods so that you may see how to pass both a string and an object reference by value. What you are doing right now is passing the object reference of one by value. It may not be needed or you may want to provide restrictions using an interface, see http://docs.oracle.com/javase/tutorial/java/concepts/interface.html
Use the constructor to pass the values
public class Two {
private String value;
public Two(String a){
this.value=a;
System.out.println("Ok "+value+", lets start!");
}
//getter and setters
}
Then while creating the instance use that constructor
Two object = new Two(name);
pass your value to the Two class constructor.
if(start != null){
Two object = new Two(start );
}
and
public Two(String s){
System.out.println("Ok "+s+", lets start!");
}
To make your code compile, move the String name variable into a static field:
public class One {
public static String name;
public static void main(String[] args){
// Note: The "name" variable is no longer defined here
String start; // etc
// rest of code the same
}
}
I'm not going to tell you this is good code design, but it does what you asked.
You will also do like this
public class One {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
retrun this.name;
}
public static void main(String[] args){
String name;
String start;
Scanner input = new Scanner(System.in);
System.out.println("Hello, what is your name?");
name = input.nextLine();
System.out.println("Hello "+name+", welcome! To ocntinue, please hit any key.");
start = input.nextLine();
if(start != null){
Two two = new Two();
two.printName(this);
}
}
class Two{
public void printName(One one){
System.out.println("" + one.getName() );
}
}

Categories

Resources