2014-10-10 65 views
0

这些是此作业的说明。任何帮助,将不胜感激。当谈到java时我是一个新手,而且似乎无法解决这个问题。对象java类代码

这个练习有两个类。第一堂课名为ObjectsToArrayList。第二类叫做Objects

您的责任是创建Object类并找出ObjectsToArrayList类的工作方式。

ObjectsToArraylist类将创建一个对象的ArrayList。它会询问并填充Object的数据字段,然后将其添加到ArrayList。这可以用于用户想要输入的多个Object实例。

对象类的要求。

  • 2个数据字段:int obj_id,String obj_name。
  • 2构造函数:No-Arg以及将这两个值都赋值并将它们赋值给数据字段的构造函数。
  • 获取并设置两个数据字段
  • 一个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) 
} 
+0

你能弄清楚什么?此外,类的名称应该是“对象”而不是“对象”。 – 2014-10-10 04:15:22

+0

第一堂课给出了,对吧?所以,尽管看起来你已经完成了大部分工作,但基本上要求我们做好你的功课...... – 2014-10-10 04:55:44

+0

“我无法弄清楚的部分”是如何编写所有课程?然后阅读关于java中的类和对象的教程。 SO是针对特定的狭窄问题,而不是针对您不知道自己在做什么的情况。 – l4mpi 2014-10-10 10:11:43

回答

0

下面是构造函数的例子:

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){ 
    } 

} 

您可以在下面的链接是指了解更多关于创建建设者和分配值字段:

http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

注:请不要使用对象的一类,因为对象是的根类层次结构。每个类都有Object作为超类。所有的对象,包括数组,都实现了这个类的方法。

http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html

0

尝试是这样的:

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); 
    } 
} 
0

好了,所以你想创建自己的对象洙有2类,而,personProfile和arrayOfProfiles

因此类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()); 
    } 
} 


}