对象java类代码

对象java类代码,java,class,object,Java,Class,Object,以下是本作业的说明。任何帮助都将不胜感激。谈到java,我是一个新手,似乎无法理解这一点 这个练习有两个班。第一个类名为ObjectsToArrayList。第二个类只称为对象 您的职责是创建对象类并了解ObjectsToArrayList类的工作方式 ObjectsToArraylist类将创建对象的ArrayList。它将请求并填充对象的数据字段,然后将其添加到数组列表。可以对用户想要输入的对象的任意多个实例执行此操作 对象类的要求 2个数据字段:int obj_id、字符串obj_nam

以下是本作业的说明。任何帮助都将不胜感激。谈到java,我是一个新手,似乎无法理解这一点

这个练习有两个班。第一个类名为
ObjectsToArrayList
。第二个类只称为
对象

您的职责是创建对象类并了解ObjectsToArrayList类的工作方式

ObjectsToArraylist
类将创建对象的
ArrayList
。它将请求并填充
对象的数据字段
,然后将其添加到
数组列表
。可以对用户想要输入的
对象的任意多个实例执行此操作

对象类的要求

  • 2个数据字段:int obj_id、字符串obj_name

  • 2个构造函数:没有参数,一个接受两个值并将它们分配给数据字段的构造函数

  • 获取和设置两个数据字段的值

  • 返回如下输出的
    toString()
    方法:

      The object ID is 22 and the name is Andrea 
    
这是我的密码

import java.util.*;
/**
 *
 * @author Student
 */
public class ObjectsToArrayList {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList <Object> objectList = new ArrayList();
        
        
        System.out.println("Please enter information for your favorite object!");
        do {  // collect an indicator to determine the method to call
            Object object = new Object();
            System.out.println("Enter a whole number for the ID of your object:\n"
                    + "or enter 99 to quit.");
             int tmpInt = input.nextInt();
            // if 99 is entered exit the loop.
            if (tmpInt == 99) {
                break;
            }
            object.setObj_id(tmpInt);
            input.nextLine();
            // ask for the Object Name
            System.out.println("Please enter the name of the Object:");
            object.setObj_name(input.nextLine());
            
            objectList.add(object);
          

        } while (true);  // this is a contineous loop if the break isn't included.
        
        for(Object object:objectList) {
            System.out.println(object.toString());
        }
    }   
}

//****************************************************
//**** Objects Class is below this block            **
//****************************************************

class Object {

      // enter object code here (This is the part I cannot figure out)
} 
import java.util.*;
/**
*
*@作者学生
*/
公共类ObjectsToArrayList{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
ArrayList objectList=新建ArrayList();
System.out.println(“请输入您喜欢的对象的信息!”);
do{//收集一个指示符以确定要调用的方法
对象=新对象();
System.out.println(“输入对象ID的整数:\n”
+“或输入99退出。”);
int tmpInt=input.nextInt();
//如果输入99,则退出循环。
如果(tmpInt==99){
打破
}
object.setObj_id(tmpInt);
input.nextLine();
//询问对象名称
System.out.println(“请输入对象的名称:”);
object.setObj_名称(input.nextLine());
objectList.add(object);
}while(true);//如果不包括中断,则这是一个连续循环。
用于(对象:对象列表){
System.out.println(object.toString());
}
}   
}
//****************************************************
//****对象类位于此块下面**
//****************************************************
类对象{
//在此处输入目标代码(这是我无法理解的部分)
} 

以下是构造函数的示例:

class Objects{
    // This is a constructor with no argument
    public Objects(){
    }

    // This is a constructor with 2 arguments
    public Objects(int obj_id, String obj_name){
    }

}
您可以参考下面的链接,了解有关创建构造函数和为字段赋值的更多信息:

注意:不要将对象用作类,因为对象是类层次结构的根。每个类都有对象作为超类。所有对象(包括数组)都实现此类的方法


试试这样的方法:

public class ObjectsToArrayList {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    ArrayList <Object> objectList = new ArrayList();


    System.out.println("Please enter information for your favorite object!");
    do {  // collect an indicator to determine the method to call
        Object object = new Object();
        System.out.println("Enter a whole number for the ID of your object:\n"
                + "or enter 99 to quit.");
         int tmpInt = input.nextInt();
        // if 99 is entered exit the loop.
        if (tmpInt == 99) {
            break;
        }
        object.setObj_id(tmpInt);//runs the setObj_id method in the Objects class 
                                 //for the users input
        input.nextLine();
        // ask for the Object Name
        System.out.println("Please enter the name of the Object:");
        object.setObj_name(input.nextLine());//runs the setObj_name method in the Objects class 
                                             //for the users input

        objectList.add(object);


    } while (true);  // this is a contineous loop if the break isn't included.
    for(Object object:objectList) {
        System.out.println(object.toString());//prints out what the toString method returns in
                                              //the Objects class
    }
}   
}

//****************************************************
//**** Objects Class is below this block            **
//****************************************************

public class Objects
{
    //add stuff like methods and instance variables
    private int ID;
    private String name;

    public Objects(){//default constructor
    } 

    public Objects(int i, String n){//constructor to change both ID and name at the same time
        ID = i;
        name = n;
    }

    public void setObj_id(int i){//constructor to change only ID
        ID = i;
    }

    public void setObj_name(String n){//constructor to change only name
        name = n;
    }

    public String toString(){
        return (name + ": " + ID);
    }
}
公共类ObjectsToArrayList{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
ArrayList objectList=新建ArrayList();
System.out.println(“请输入您喜欢的对象的信息!”);
do{//收集一个指示符以确定要调用的方法
对象=新对象();
System.out.println(“输入对象ID的整数:\n”
+“或输入99退出。”);
int tmpInt=input.nextInt();
//如果输入99,则退出循环。
如果(tmpInt==99){
打破
}
setObj_id(tmpInt);//在Objects类中运行setObj_id方法
//供用户输入
input.nextLine();
//询问对象名称
System.out.println(“请输入对象的名称:”);
object.setObj_name(input.nextLine());//在Objects类中运行setObj_name方法
//供用户输入
objectList.add(object);
}while(true);//如果不包括中断,则这是一个连续循环。
用于(对象:对象列表){
System.out.println(object.toString());//输出toString方法在
//对象类
}
}   
}
//****************************************************
//****对象类位于此块下面**
//****************************************************
公共类对象
{
//添加方法和实例变量之类的东西
私有int-ID;
私有字符串名称;
公共对象(){//默认构造函数
} 
公共对象(inti,String n){//constructor,用于同时更改ID和名称
ID=i;
name=n;
}
public void setObj_id(int i){//构造函数仅更改id
ID=i;
}
public void setObj_name(字符串n){//构造函数仅更改名称
name=n;
}
公共字符串toString(){
返回(名称+”:“+ID);
}
}

好的,所以您想创建自己的对象,所以有两个类,personProfile和ArrayOfProfile

所以类personDetails对象注释分开类文件不*不在同一文件中

class personProfile{
private int ID = 0;
private String Name = "";

  public personProfile(int id,String name){
    ID = id;
     Name = name; 
  }

 public int getID(){
   return ID;
 }
 public String getName(){
   return name;
 }

}
现在是另一个类*不同的文件

class arrayOfProfiles{
  personProfile profiles[] = new personProfile[0];

  public personProfile[] array(){
    return profiles;
  }
  public void addProfileToArray(personProfile profileObject){
    personProfile arrayMod = new personProfile[profiles.length+1];

    for(int i = 0;i<profiles.length;i++){
      arrayMod[i] = profiles[i];
    }
    arrayMod[arrayMod.length-1] = profileObject;
  }

public static void main(String args[]){
  arrayOfProfiles profiles = new arrayOfProfiles();

  for(int i = 0;i<5;i++){
    int id = Integer.parseInt(JOptionPane.showInputDialog("enter ID ");
    String name = JOptionPane.showInputDialog("enter name");

    profiles.addProfileToArray(new personProfile(id,name));
  }
  for(int i =0;i<profiles.array().length;i++){
    System.out.println("ID :"+profiles[i].getID()+" name - "+profiles[i].getName());
  }
}


}
类阵列文件{
personProfile profiles[]=新的personProfile[0];
public personProfile[]数组(){
返回配置文件;
}
public void addProfileToArray(personProfile profileObject){
personProfile arrayMod=新的personProfile[profiles.length+1];

对于(int i=0;我你不明白什么?另外,类的名称应该是
Objects
,而不是
Object
。第一个类是给定的,对吗?因此,尽管看起来你已经完成了大部分工作,但你基本上要求我们做家庭作业……”我不明白的部分是如何编写所有的类?然后去读一本关于类和对象的教程