1 / 18

ITK:P1 Lektion 1

ITK:P1 Lektion 1. Klasser, datatyper och selektion. DSV Peter Mozelius. Lektion 11. public class Lektion11 { public static void main(String[] args) { System.out.println("Hello World!"); } //main } //Lektion11. Lektion 12. public class Hello { private String greeting ="Hello ";

melva
Télécharger la présentation

ITK:P1 Lektion 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITK:P1 Lektion 1 Klasser, datatyper och selektion DSV Peter Mozelius

  2. Lektion 11 public class Lektion11 { public static void main(String[] args) { System.out.println("Hello World!"); }//main }//Lektion11

  3. Lektion 12 public class Hello { private String greeting ="Hello "; public Hello(){ greeting = greeting + "World!"; }//defaultkonstruktor public Hello(String place){ greeting = greeting + place; }//konstruktor som tar argument

  4. Lektion 12 public String sayHello(){ return greeting; }//sayHello }//Hello

  5. Lektion 12 public class Lektion12 { public static void main(String[] args){ //En hälsning där platsen ändras Hello hello1 = new Hello("Kista"); String message = hello1.sayHello() + "!"; System.out.println(message); //En hälsning med defaultkonstruktorn Hello hello2 = new Hello(); System.out.println(hello2.sayHello());

  6. Lektion 12 }//main }//Lektion12

  7. Lektion 13 import java.text.*; import javax.swing.*; public class Lektion13 { public static void main(String[] args) { String indata; double dividend, divisor, kvot;

  8. Lektion 13 //läs in det tal som ska divideras indata = JOptionPane.showInputDialog( "Skriv in din DIVIDEND: "); dividend = Double.parseDouble(indata); //läs in divisorn indata = JOptionPane.showInputDialog( "Skriv in din DIVISOR: "); divisor = Double.parseDouble(indata);

  9. Lektion 13 //om divisorn inte är 0 så utför divisionen if(divisor == 0){ JOptionPane.showMessageDialog(null, "Divison med 0", "Lektion13", JOptionPane.ERROR_MESSAGE); System.exit(666); } else{ kvot = dividend / divisor;

  10. Lektion 13 //formatera resultatet String mönster = "###.##"; DecimalFormat df = new DecimalFormat(mönster); String resultat = df.format(kvot); //visa resultatet JOptionPane.showMessageDialog(null, "Division utförd: " + dividend + " / " + divisor + " = " + resultat); } System.exit(0);

  11. Lektion 13 }//main }//Lektion13

  12. Lektion 14 public class Figur { private int hörn; public Figur(int hörn){ this.hörn = hörn; }//konstruktor public int visaAntalHörn() { return hörn; }//visaAntalHörn }//Figur

  13. Lektion 14 import javax.swing.*; public class Lektion14 { public static void main(String[] args) { String typ; //skapa en geometrisk figur String indata = JOptionPane.showInputDialog( "Hur många hörn ska din figur ha (minst 1)"); int hörn = Integer.parseInt(indata);

  14. Lektion 14 if(hörn > 0){ Figur figur = new Figur(hörn); //en switch-sats kollar vad det blev för figur switch(figur.visaAntalHörn()) { case 1: typ = new String("Figuren är en punkt."); break; case 2: typ = new String("Figuren är en linje."); break;

  15. Lektion 14 case 3: typ = new String("Figuren är en triangel."); break; case 4: typ = new String("Figuren är en rektangel."); break; case 5: typ = new String("Figuren är en pentagon."); break;

  16. Lektion 14 case 6: typ = new String("Figuren är en hexagon."); break; case 7: typ = new String("Figuren är en heptagon."); break; case 8: typ = new String("Figuren är en oktagon."); break;

  17. Lektion 14 default: typ = new String("Figuren är en polygon."); }//switch }//if else typ = new String("Antalet hörn måste vara större än noll"); JOptionPane.showMessageDialog(null, typ); System.exit(0);

  18. Lektion 14 }//main }//Lektion14 Specifikationer av Javas färdiga klasser: http://java.sun.com/javase/6/docs/api/index.html API = Application Programmable Interface

More Related