Initialisierungen der einzelnen Klassen - Objektorientierung

turmalex8

Neues Mitglied
Hallo,

ich fange gerade erst an mit der Objektorientiertung und habe folgende Zeilen programmiert:
mein probleme sind, dass ich nicht weiß wohin ich Shop shop = new Shop () platzieren soll, ebenso das Phone array und bei der Ausgabe in der Klasse shop eine Methoder der Klasse phone aufzurufen, ich habs mit phone.printPhone gemacht, aber das funktioniert nicht...
Sorry für die Unübersichtlichkeit, aber ich habe mich eben erst angemeldet und weiß noch nicht wie ich das hier besser darstellen kann. Ich hoffe es kann mir jemand helfen.
Danke schonmal im Voraus.

public class SmartphoneShop
{
public class Phone
{
private String phoneName, cpu, storage, resolution, os, weight;
private int productNumber;

public void phoneName (String phoneName)
{
this.phoneName = phoneName;

} //end phoneName

public void setCpu (String cpu) //cpu setter
{
this.cpu = cpu;

} //end setName

public String getCpu () //cpu getter
{
return this.cpu;

} //end getName

//hier stehen dann die restlichen setter und getter


public void printPhone()
{
System.out.println (); //hier soll dann die Ausgabe erfolgen


} //end printPhone

} //end Phone

public class Shop
{
Shop shop = new Shop(); //initialize the shop

private Phone[] phones = new Phone[3]; //initialize Phone[] storing for example 3 phones

public void printPhones()
{

for (int i = 1; i <= phones.length; i++)
{
System.out.println ("Phone " + i + ":\n" + phone.printPhone());

}

} //end printPhones

} //end Shop

public static void printInfo ()
{
System.out.println ("\t\t\t\tSmartphone Shop");
System.out.println ("\t\t\t\t---------------");

} //end printInfo

public static void main (String[] args)
{
//START OF PROGRAM



printInfo ();

Phone[] phones = new Phone[5];

Phone apple = new Phone ("Apple iPhone 5");
apple.setCpu ("1.3 GHz dual core Apple A6");
apple.setOs ("iOS 6.0.1");
apple.setResolution ("640x1136");
apple.setStorage ("64 GB");
apple.setWeight ("112 g");

phones[0] = apple;

Phone samsung = new Phone ("Samsung Galaxy S III");
samsung.setCpu ("1.4 GHz quad core Cortex-A9");
samsung.setOs ("Android 4.0 (Ice Cream Sandwich");
samsung.setResolution ("720x1280");
samsung.setStorage ("32 GB");
samsung.setWeight ("133 g");

phones[1] = samsung;

Phone sony = new Phone ("Sony Xperia T");
sony.setCpu ("1.5 GHz dual core Qualcomm Krait MSM8260-A");
sony.setOs ("Android 4.0 (Ice Cream Sandwich");
sony.setResolution ("1280x720");
sony.setStorage ("16 GB");
sony.setWeight ("139 g");

phones[2] = sony;


shop.printPhones();



//END OF PROGRAM

} //end main

} //end SmartphoneShop
 
Der dicke, rote JAVA-TAGS Button, war wohl wieder mal nicht groß genug...
Warum alles in eine Klasse? Es wäre übersichtlicher gewesen Phone in eine eigene zu verschieben.

Java:
public class SmartphoneShop
{
public class Phone
{
private String phoneName, cpu, storage, resolution, os, weight;
private int productNumber;

public void phoneName (String phoneName)
{
this.phoneName = phoneName;

} //end phoneName

public void setCpu (String cpu) //cpu setter
{
this.cpu = cpu;

} //end setName

public String getCpu () //cpu getter
{
return this.cpu;

} //end getName

//hier stehen dann die restlichen setter und getter


public void printPhone()
{
System.out.println (); //hier soll dann die Ausgabe erfolgen


} //end printPhone

} //end Phone

public class Shop
{
Shop shop = new Shop(); //initialize the shop

private Phone[] phones = new Phone[3]; //initialize Phone[] storing for example 3 phones

public void printPhones()
{

for (int i = 1; i <= phones.length; i++)
{
System.out.println ("Phone " + i + ":\n" + phone.printPhone());

}

} //end printPhones

} //end Shop

public static void printInfo ()
{
System.out.println ("\t\t\t\tSmartphone Shop");
System.out.println ("\t\t\t\t---------------");

} //end printInfo

public static void main (String[] args)
{
//START OF PROGRAM



printInfo ();

Phone[] phones = new Phone[5];

Phone apple = new Phone ("Apple iPhone 5");
apple.setCpu ("1.3 GHz dual core Apple A6");
apple.setOs ("iOS 6.0.1");
apple.setResolution ("640x1136");
apple.setStorage ("64 GB");
apple.setWeight ("112 g");

phones[0] = apple;

Phone samsung = new Phone ("Samsung Galaxy S III");
samsung.setCpu ("1.4 GHz quad core Cortex-A9");
samsung.setOs ("Android 4.0 (Ice Cream Sandwich");
samsung.setResolution ("720x1280");
samsung.setStorage ("32 GB");
samsung.setWeight ("133 g");

phones[1] = samsung;

Phone sony = new Phone ("Sony Xperia T");
sony.setCpu ("1.5 GHz dual core Qualcomm Krait MSM8260-A");
sony.setOs ("Android 4.0 (Ice Cream Sandwich");
sony.setResolution ("1280x720");
sony.setStorage ("16 GB");
sony.setWeight ("139 g");

phones[2] = sony;


shop.printPhones();



//END OF PROGRAM

} //end main

} //end SmartphoneShop
 
Ich habe ihn schon gesehen, wusste aber nicht genau wie der funktioniert...weil man sieht ja hier nicht, wie das dann dargestellt wird, oder?

Also, wie sieht das nun aus mit der Initialisierung in den einzelnen Klassen? Ich weiß, dass es so nicht stimmt...
 
Was ich direkt sehe: Phone hat keinen Consturktor, der einen String entgegen nimmt, dennoch rufst du
Java:
	Phone apple = new Phone ("Apple iPhone 5");
auf. Das kann schonmal nicht funktionieren. Einige Getter/ Setter heißen nicht so,m wie du sie aufrufst, das geht auch nicht.

Nun einmal alles in 3 Klassen:

Java:
public class Phone
	{
		private String	phoneName, cpu, storage, resolution, os, weight;
		private int		productNumber;

		/**
		 * This is the default constructor of this class.
		 */
		public Phone(String name)
		{
			phoneName = name;
		}
		
		public void setPhoneName(String phoneName)
		{
			this.phoneName = phoneName;

		} // end phoneName

		public void setCpu(String cpu) // cpu setter
		{
			this.cpu = cpu;

		} // end setName

		public String getCpu() // cpu getter
		{
			return this.cpu;

		} // end getName

		// hier stehen dann die restlichen setter und getter

		public void printPhone()
		{
			System.out.println(); // hier soll dann die Ausgabe erfolgen

		} // end printPhone

		public String getStorage()
		{
			return storage;
		}

		public void setStorage(String storage)
		{
			this.storage = storage;
		}

		public String getResolution()
		{
			return resolution;
		}

		public void setResolution(String resolution)
		{
			this.resolution = resolution;
		}

		public String getOs()
		{
			return os;
		}

		public void setOs(String os)
		{
			this.os = os;
		}

		public String getWeight()
		{
			return weight;
		}

		public void setWeight(String weight)
		{
			this.weight = weight;
		}

		public int getProductNumber()
		{
			return productNumber;
		}

		public void setProductNumber(int productNumber)
		{
			this.productNumber = productNumber;
		}

		public String getPhoneName()
		{
			return phoneName;
		}
	}

Java:
public class Shop
{
	Shop	data	= new Shop();
	private Phone[] phones = new Phone[3];
	
	// for example 3 phones
	public void printPhones()
	{
		for (int i = 1; i <= phones.length; i++)
		{
			System.out.println("Phone " + i + ":\n" + phone.printPhone());
		}
	} // end printPhones
}

Java:
public class SmartphoneShop
{
	public static void printInfo()
	{
		System.out.println("\t\t\t\tSmartphone Shop");
		System.out.println("\t\t\t\t---------------");

	} // end printInfo

	public static void main(String[] args)
	{
		// START OF PROGRAM
		printInfo();

		Phone[] phones = new Phone[5];

		Phone apple = new Phone("Apple iPhone 5");
		apple.setCpu("1.3 GHz dual core Apple A6");
		apple.setOs("iOS 6.0.1");
		apple.setResolution("640x1136");
		apple.setStorage("64 GB");
		apple.setWeight("112 g");

		phones[0] = apple;

		Phone samsung = new Phone("Samsung Galaxy S III");
		samsung.setCpu("1.4 GHz quad core Cortex-A9");
		samsung.setOs("Android 4.0 (Ice Cream Sandwich");
		samsung.setResolution("720x1280");
		samsung.setStorage("32 GB");
		samsung.setWeight("133 g");

		phones[1] = samsung;

		Phone sony = new Phone("Sony Xperia T");
		sony.setCpu("1.5 GHz dual core Qualcomm Krait MSM8260-A");
		sony.setOs("Android 4.0 (Ice Cream Sandwich");
		sony.setResolution("1280x720");
		sony.setStorage("16 GB");
		sony.setWeight("139 g");

		phones[2] = sony;

		shop.printPhones();

		// END OF PROGRAM
	} // end main

}
 
Zuletzt bearbeitet:
Okay, also dann wollen wir uns mal ran setzten, generell würde ich mir das so denken:

-Du erstellst einen Shop.
-Dieser Shop weiß, welche Phones er verkauft (er bekommt diese über eine Methode übergeben).
-Du kannst den Shop fragen, welche Phones er verkauft (shop.printPhones()).
-Jedes Phone kann eine textuelle Beschreibung von sich selbst zurückgeben (phone.printPhone(), achtung, das sollte ein String zurückgeben)

Mal etwas pseudocode:
PHP:
main:
  shop = new Shop();
  shop.addPhone(new Phone(...));
  ...
  shop.printPhones();
 

Zurück
Oben